<commit_before>////////////////////////////////////////////////////////////////////////////////
/// @brief Aql, V8 expression
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2014 ArangoDB GmbH, Cologne, Germany
/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
///     http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author <NAME>
/// @author Copyright 2014, ArangoDB GmbH, Cologne, Germany
/// @author Copyright 2012-2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////

#include "Aql/V8Expression.h"
#include "Aql/AqlItemBlock.h"
#include "Aql/Executor.h"
#include "Aql/Query.h"
#include "Aql/Variable.h"
#include "Basics/json.h"
#include "Basics/json-utilities.h"
#include "V8/v8-conv.h"
#include "V8/v8-utils.h"

using namespace triagens::aql;

// -----------------------------------------------------------------------------
// --SECTION--                                        constructors / destructors
// -----------------------------------------------------------------------------

////////////////////////////////////////////////////////////////////////////////
/// @brief create the v8 expression
////////////////////////////////////////////////////////////////////////////////

V8Expression::V8Expression (v8::Isolate* isolate,
                            v8::Handle<v8::Function> func,
                            v8::Handle<v8::Object> constantValues,
                            bool isSimple)
  : isolate(isolate),
    _func(),
    _constantValues(),
    _isSimple(isSimple) {

  _func.Reset(isolate, func);
  _constantValues.Reset(isolate, constantValues);
}

////////////////////////////////////////////////////////////////////////////////
/// @brief destroy the v8 expression
////////////////////////////////////////////////////////////////////////////////

V8Expression::~V8Expression () {
  _constantValues.Reset();
  _func.Reset();
}

// -----------------------------------------------------------------------------
// --SECTION--                                                    public methods
// -----------------------------------------------------------------------------

////////////////////////////////////////////////////////////////////////////////
/// @brief execute the expression
////////////////////////////////////////////////////////////////////////////////

AqlValue V8Expression::execute (v8::Isolate* isolate,
                                Query* query,
                                triagens::arango::AqlTransaction* trx,
                                AqlItemBlock const* argv,
                                size_t startPos,
                                std::vector<Variable*> const& vars,
                                std::vector<RegisterId> const& regs) {
  size_t const n = vars.size();
  TRI_ASSERT_EXPENSIVE(regs.size() == n); // assert same vector length

  bool const hasRestrictions = ! _attributeRestrictions.empty();

  v8::Handle<v8::Object> values = v8::Object::New(isolate);

  for (size_t i = 0; i < n; ++i) {
    auto reg = regs[i];

    auto const& value(argv->getValueReference(startPos, reg)); 

    if (value.isEmpty()) {
      continue;
    }
    
    auto document = argv->getDocumentCollection(reg);
    auto const& varname = vars[i]->name;

    if (hasRestrictions && value.isJson()) {
      // check if we can get away with constructing a partial JSON object
      auto it = _attributeRestrictions.find(vars[i]);

      if (it != _attributeRestrictions.end()) {
        // build a partial object
        values->ForceSet(TRI_V8_STD_STRING(varname), value.toV8Partial(isolate, trx, (*it).second, document));
        continue;
      }
    }

    // fallthrough to building the complete object

    // build the regular object
    values->ForceSet(TRI_V8_STD_STRING(varname), value.toV8(isolate, trx, document));
  }

  TRI_ASSERT(query != nullptr);


  TRI_GET_GLOBALS();
    
  v8::Handle<v8::Value> result;

  auto old = v8g->_query;

  try {
    v8g->_query = static_cast<void*>(query);
    TRI_ASSERT(v8g->_query != nullptr);

    // set function arguments
    auto constantValues = v8::Local<v8::Object>::New(isolate, _constantValues);<commit_msg>added code comment<commit_after>////////////////////////////////////////////////////////////////////////////////
/// @brief Aql, V8 expression
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2014 ArangoDB GmbH, Cologne, Germany
/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
///     http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author <NAME>
/// @author Copyright 2014, ArangoDB GmbH, Cologne, Germany
/// @author Copyright 2012-2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////

#include "Aql/V8Expression.h"
#include "Aql/AqlItemBlock.h"
#include "Aql/Executor.h"
#include "Aql/Query.h"
#include "Aql/Variable.h"
#include "Basics/json.h"
#include "Basics/json-utilities.h"
#include "V8/v8-conv.h"
#include "V8/v8-utils.h"

using namespace triagens::aql;

// -----------------------------------------------------------------------------
// --SECTION--                                        constructors / destructors
// -----------------------------------------------------------------------------

////////////////////////////////////////////////////////////////////////////////
/// @brief create the v8 expression
////////////////////////////////////////////////////////////////////////////////

V8Expression::V8Expression (v8::Isolate* isolate,
                            v8::Handle<v8::Function> func,
                            v8::Handle<v8::Object> constantValues,
                            bool isSimple)
  : isolate(isolate),
    _func(),
    _constantValues(),
    _isSimple(isSimple) {

  _func.Reset(isolate, func);
  _constantValues.Reset(isolate, constantValues);
}

////////////////////////////////////////////////////////////////////////////////
/// @brief destroy the v8 expression
////////////////////////////////////////////////////////////////////////////////

V8Expression::~V8Expression () {
  _constantValues.Reset();
  _func.Reset();
}

// -----------------------------------------------------------------------------
// --SECTION--                                                    public methods
// -----------------------------------------------------------------------------

////////////////////////////////////////////////////////////////////////////////
/// @brief execute the expression
////////////////////////////////////////////////////////////////////////////////

AqlValue V8Expression::execute (v8::Isolate* isolate,
                                Query* query,
                                triagens::arango::AqlTransaction* trx,
                                AqlItemBlock const* argv,
                                size_t startPos,
                                std::vector<Variable*> const& vars,
                                std::vector<RegisterId> const& regs) {
  size_t const n = vars.size();
  TRI_ASSERT_EXPENSIVE(regs.size() == n); // assert same vector length

  bool const hasRestrictions = ! _attributeRestrictions.empty();

  v8::Handle<v8::Object> values = v8::Object::New(isolate);

  for (size_t i = 0; i < n; ++i) {
    auto reg = regs[i];

    auto const& value(argv->getValueReference(startPos, reg)); 

    if (value.isEmpty()) {
      continue;
    }
    
    auto document = argv->getDocumentCollection(reg);
    auto const& varname = vars[i]->name;

    if (hasRestrictions && value.isJson()) {
      // check if we can get away with constructing a partial JSON object
      auto it = _attributeRestrictions.find(vars[i]);

      if (it != _attributeRestrictions.end()) {
        // build a partial object
        values->ForceSet(TRI_V8_STD_STRING(varname), value.toV8Partial(isolate, trx, (*it).second, document));
        continue;
      }
    }

    // fallthrough to building the complete object

    // build the regular object
    values->ForceSet(TRI_V8_STD_STRING(varname), value.toV8(isolate, trx, document));
  }

  TRI_ASSERT(query != nullptr);


  TRI_GET_GLOBALS();
    
  v8::Handle<v8::Value> result;

  auto old = v8g->_query;

  try {
    v8g->_query = static_cast<void*>(query);
    TRI_ASSERT(v8g->_query != nullptr);

    // set constant function arguments
    // note: constants are passed by reference so we can save re-creating them
    // on every invocation. this however means that these constants must not be
    // modified by the called function. there is a hash check in place below to
    // verify that constants don't get modified by the called function. 
    // note: user-defined AQL functions are always called without constants
    // because they are opaque to the optimizer and the assumption that they
    // won't modify their arguments is unsafe
    auto constantValues = v8::Local<v8::Object>::New(isolate, _constantValues);
{'fraction_non_alphanumeric': 0.2886118531710696, 'fraction_numerical': 0.012268724646284753, 'mean_word_length': 1.3517915309446253, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3599172', 'n_tokens_mistral': 2539, 'n_tokens_neox': 2401, 'n_words': 689}