1
0
Fork 0
arangodb/lib/V8/v8-execution.cpp

168 lines
5.7 KiB
C++

////////////////////////////////////////////////////////////////////////////////
/// @brief V8 utility functions
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2004-2012 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 triAGENS GmbH, Cologne, Germany
///
/// @author Dr. Frank Celler
/// @author Copyright 2011-2012, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#include "v8-execution.h"
#include <fstream>
#include <locale>
#include <v8.h>
#include "V8/v8-conv.h"
using namespace std;
// -----------------------------------------------------------------------------
// --SECTION-- EXECUTION CONTEXT
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// --SECTION-- public types
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup V8Utils
/// @{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief execution context
////////////////////////////////////////////////////////////////////////////////
typedef struct js_exec_context_s {
v8::Persistent<v8::Function> _func;
v8::Persistent<v8::Object> _arguments;
}
js_exec_context_t;
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- constructors and destructors
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup V8Utils
/// @{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief creates a new execution context
////////////////////////////////////////////////////////////////////////////////
TRI_js_exec_context_t TRI_CreateExecutionContext (char const* script) {
js_exec_context_t* ctx;
// execute script inside the context
v8::Handle<v8::Script> compiled = v8::Script::Compile(v8::String::New(script),
v8::String::New("--script--"));
// compilation failed, print errors that happened during compilation
if (compiled.IsEmpty()) {
return 0;
}
// compute the function
v8::Handle<v8::Value> val = compiled->Run();
if (val.IsEmpty()) {
return 0;
}
ctx = new js_exec_context_t;
ctx->_func = v8::Persistent<v8::Function>::New(v8::Handle<v8::Function>::Cast(val));
ctx->_arguments = v8::Persistent<v8::Object>::New(v8::Object::New());
// return the handle
return (TRI_js_exec_context_t) ctx;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief frees an new execution context
////////////////////////////////////////////////////////////////////////////////
void TRI_FreeExecutionContext (TRI_js_exec_context_t context) {
js_exec_context_t* ctx;
ctx = (js_exec_context_t*) context;
ctx->_func.Dispose();
ctx->_func.Clear();
ctx->_arguments.Dispose();
ctx->_arguments.Clear();
delete ctx;
}
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- public functions
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup V8Utils
/// @{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief executes a result context
////////////////////////////////////////////////////////////////////////////////
TRI_json_t* TRI_ExecuteResultContext (TRI_js_exec_context_t context) {
js_exec_context_t* ctx;
ctx = (js_exec_context_t*) context;
// convert back into a handle
v8::Persistent<v8::Function> func = ctx->_func;
// and execute the function
v8::Handle<v8::Value> args[] = { ctx->_arguments };
v8::Handle<v8::Value> result = func->Call(func, 1, args);
if (result.IsEmpty()) {
return NULL;
}
return TRI_ObjectToJson(result);
}
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// Local Variables:
// mode: outline-minor
// outline-regexp: "^\\(/// @brief\\|/// {@inheritDoc}\\|/// @addtogroup\\|/// @page\\|// --SECTION--\\|/// @\\}\\)"
// End: