1
0
Fork 0
arangodb/arangod/Aql/V8Expression.cpp

166 lines
5.6 KiB
C++

////////////////////////////////////////////////////////////////////////////////
/// @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 Jan Steemann
/// @author Copyright 2014, ArangoDB GmbH, Cologne, Germany
/// @author Copyright 2012-2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#include "Aql/V8Expression.h"
#include "Aql/Executor.h"
#include "Aql/Query.h"
#include "Aql/Variable.h"
#include "Basics/json.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)
: isolate(isolate),
_func() {
_func.Reset(isolate, func);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief destroy the v8 expression
////////////////////////////////////////////////////////////////////////////////
V8Expression::~V8Expression () {
_func.Reset();
}
// -----------------------------------------------------------------------------
// --SECTION-- public methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief execute the expression
////////////////////////////////////////////////////////////////////////////////
AqlValue V8Expression::execute (v8::Isolate* isolate,
Query* query,
triagens::arango::AqlTransaction* trx,
std::vector<TRI_document_collection_t const*>& docColls,
std::vector<AqlValue>& 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 const varname = vars[i]->name;
auto reg = regs[i];
TRI_ASSERT_EXPENSIVE(! argv[reg].isEmpty());
if (hasRestrictions && argv[startPos + reg]._type == AqlValue::JSON) {
// 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), argv[startPos + reg].toV8Partial(isolate, trx, (*it).second, docColls[reg]));
continue;
}
}
// fallthrough to building the complete object
// build the regular object
values->ForceSet(TRI_V8_STD_STRING(varname), argv[startPos + reg].toV8(isolate, trx, docColls[reg]));
}
TRI_ASSERT(query != nullptr);
TRI_GET_GLOBALS();
auto old = v8g->_query;
v8g->_query = static_cast<void*>(query);
TRI_ASSERT(v8g->_query != nullptr);
// set function arguments
v8::Handle<v8::Value> args[] = { values };
// execute the function
v8::TryCatch tryCatch;
auto func = v8::Local<v8::Function>::New(isolate, _func);
v8::Handle<v8::Value> result = func->Call(func, 1, args);
v8g->_query = old;
Executor::HandleV8Error(tryCatch, result);
// no exception was thrown if we get here
TRI_json_t* json = nullptr;
if (result->IsUndefined()) {
// expression does not have any (defined) value. replace with null
json = TRI_CreateNullJson(TRI_UNKNOWN_MEM_ZONE);
}
else {
// expression had a result. convert it to JSON
json = TRI_ObjectToJson(isolate, result);
// TODO: json = TRI_SimplifiedObjectToJson(isolate, result);
}
if (json == nullptr) {
THROW_ARANGO_EXCEPTION(TRI_ERROR_OUT_OF_MEMORY);
}
try {
auto j = new triagens::basics::Json(TRI_UNKNOWN_MEM_ZONE, json);
return AqlValue(j);
}
catch (...) {
// prevent memleak
TRI_FreeJson(TRI_UNKNOWN_MEM_ZONE, json);
throw;
}
}
// -----------------------------------------------------------------------------
// --SECTION-- END-OF-FILE
// -----------------------------------------------------------------------------
// Local Variables:
// mode: outline-minor
// outline-regexp: "/// @brief\\|/// {@inheritDoc}\\|/// @page\\|// --SECTION--\\|/// @\\}"
// End