/* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2.0, as published by the Free Software Foundation. This program is also distributed with certain software (including but not limited to OpenSSL) that is licensed under separate terms, as designated in a particular file or component or in included license documentation. The authors of MySQL hereby grant you an additional permission to link the program and your derivative works with the separately licensed software that they have included with MySQL. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef NODEJS_ADAPTER_INCLUDE_JSCONVERTER_H #define NODEJS_ADAPTER_INCLUDE_JSCONVERTER_H #include "JsWrapper.h" #include "node_buffer.h" typedef Local jsvalue; typedef Handle jsobject; /***************************************************************** JsValueConverter Value conversion from JavaScript to C Some additional NDB-Specific specializations are in NdBJsConverters.h ******************************************************************/ /* Generic (for pointers). If you get an "invalid static cast from type void *", then the compiler may be erroneously falling back on this implementation. */ template class JsValueConverter { public: JsValueConverter(jsvalue v) { if(v->IsNull()) { native_object = 0; } else { DEBUG_ASSERT(v->IsObject()); Local obj = v->ToObject(); DEBUG_ASSERT(obj->InternalFieldCount() == 2); native_object = unwrapPointer(obj); } } virtual ~JsValueConverter() {} virtual T toC() { return native_object; } protected: T native_object; }; /**** Specializations *****/ template <> class JsValueConverter { public: jsvalue jsval; JsValueConverter(jsvalue v) : jsval(v) {}; int toC() { return jsval->Int32Value(); }; }; template <> class JsValueConverter { public: jsvalue jsval; JsValueConverter(jsvalue v) : jsval(v) {}; int toC() { return jsval->Uint32Value(); }; }; template <> class JsValueConverter { public: jsvalue jsval; JsValueConverter(jsvalue v) : jsval(v) {}; int64_t toC() { return jsval->IntegerValue(); }; }; template <> class JsValueConverter { public: jsvalue jsval; JsValueConverter(jsvalue v) : jsval(v) {}; double toC() { return jsval->NumberValue(); }; }; template <> class JsValueConverter { public: jsvalue jsval; JsValueConverter(jsvalue v) : jsval(v) {}; int64_t toC() { return jsval->IntegerValue(); }; }; template <> class JsValueConverter { public: jsvalue jsval; JsValueConverter(jsvalue v) : jsval(v) {}; bool toC() { return jsval->BooleanValue(); }; }; /* const char * is JavaScript String */ template <> class JsValueConverter { private: v8::String::Utf8Value av; public: JsValueConverter(jsvalue v) : av(v) {}; const char * toC() { return *av; }; }; /* char * is Node::Buffer */ template <> class JsValueConverter { public: jsvalue jsval; JsValueConverter(jsvalue v) : jsval(v) {}; char * toC() { DEBUG_PRINT_DETAIL("Unwrapping Node buffer"); return node::Buffer::Data(jsval->ToObject()); }; }; /* Pass through of JavaScript value */ template <> class JsValueConverter > { public: Local jsval; JsValueConverter(Local v) { jsval = Local::New(v8::Isolate::GetCurrent(), Local::Cast(v)); }; Handle toC() { return jsval; }; }; /***************************************************************** toJs functions Value Conversion from C to JavaScript These are called from the ReturnValueHandler for non-pointer types. We do not expect them ever to be called with pointer types. The generic function treats its argument as a JS Integer. SPECIALIZATIONS should be over C PRIMITIVE types only. These functions do not declare a HandleScope ******************************************************************/ template Local toJS(Isolate * isolate, T cval) { return v8::Integer::New(isolate, cval); } // unsigned int template <> inline Local toJS(Isolate * isolate, unsigned int cval) { return v8::Integer::NewFromUnsigned(isolate, cval); } // unsigned long long template <> inline Local toJS(Isolate * isolate, unsigned long long cval) { double d = static_cast(cval); return v8::Number::New(isolate, d); } // unsigned short template <> inline Local toJS(Isolate * isolate, unsigned short cval) { return v8::Integer::NewFromUnsigned(isolate, cval); } // double template <> inline Local toJS(Isolate * isolate, double cval) { return v8::Number::New(isolate, cval); }; // const char * template <> inline Local toJS(Isolate * isolate, const char * cval) { return v8::String::NewFromUtf8(isolate, cval); } // const bool * template <> inline Local toJS(Isolate * isolate, const bool * cbp) { // return Local::New(Boolean::New(isolate, *cbp)); return *cbp ? True(isolate) : False(isolate); } // bool template <> inline Local toJS(Isolate * isolate, bool b) { // return Local::New(Boolean::New(isolate, b)); return b ? True(isolate) : False(isolate); } #endif