RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
soa/js/testing/js2json_module.cc
00001 /* v8_inheritance_module.cc                                        -*- C++ -*-
00002    Jeremy Barnes, 26 July 2010
00003    Copyright (c) 2010 Datacratic.  All rights reserved.
00004 
00005    Module to test v8 inheritance from C++.
00006 */
00007 
00008 #include <signal.h>
00009 #include "soa/js/js_wrapped.h"
00010 #include "soa/js/js_utils.h"
00011 #include "soa/js/js_registry.h"
00012 #include "v8.h"
00013 #include "jml/compiler/compiler.h"
00014 #include "jml/utils/smart_ptr_utils.h"
00015 #include "soa/jsoncpp/json.h"
00016 
00017 using namespace v8;
00018 using namespace std;
00019 using namespace Datacratic;
00020 using namespace Datacratic::JS;
00021 
00022 
00023 
00024 struct FromTo {
00025     virtual ~FromTo()
00026     {
00027     }
00028 };
00029 
00030 const char * FromToName = "FromTo";
00031 const char * FromToModule = "ft";
00032 
00033 struct FromToJS
00034     : public JSWrapped2<FromTo, FromToJS, FromToName, FromToModule> {
00035 
00036     FromToJS(v8::Handle<v8::Object> This,
00037              const std::shared_ptr<FromTo> & fromto
00038              = std::shared_ptr<FromTo>())
00039     {
00040         HandleScope scope;
00041         wrap(This, fromto);
00042     }
00043 
00044     static Persistent<v8::FunctionTemplate>
00045     Initialize()
00046     {
00047         Persistent<FunctionTemplate> t = Register(New, Setup);
00048 
00049         // Instance methods
00050         NODE_SET_PROTOTYPE_METHOD(t, "roundTrip", roundTrip);
00051         NODE_SET_PROTOTYPE_METHOD(t, "getJSON1", getJSON1);
00052         NODE_SET_PROTOTYPE_METHOD(t, "getJSON2", getJSON2);
00053         NODE_SET_PROTOTYPE_METHOD(t, "getLongLongInt", getLongLongInt);
00054         NODE_SET_PROTOTYPE_METHOD(t, "getJSONLongLongInt", getJSONLongLongInt);
00055 
00056         return t;
00057     }
00058 
00059     static Handle<Value>
00060     New(const Arguments & args)
00061     {
00062         try {
00063             new FromToJS(args.This(), ML::make_std_sp(new FromTo()));
00064             return args.This();
00065         } HANDLE_JS_EXCEPTIONS;
00066     }
00067 
00068     static Handle<v8::Value>
00069     roundTrip(const Arguments & args)
00070     {
00071         try {
00072             Json::Value val = getArg(args, 0, "arg");
00073             JSValue result;
00074             to_js(result, val);
00075             return result;
00076         } HANDLE_JS_EXCEPTIONS;
00077     }
00078 
00079     static Handle<v8::Value>
00080     getJSON1(const Arguments & args)
00081     {
00082         try {
00083             Json::Value val;
00084             val["a"] = 1;
00085             JSValue result;
00086             to_js(result, val);
00087             return result;
00088         } HANDLE_JS_EXCEPTIONS;
00089     }
00090 
00091     static Handle<v8::Value>
00092     getJSON2(const Arguments & args)
00093     {
00094         try {
00095             Json::Value val;
00096             val["a"]["b"][0u] = 1;
00097             val["a"]["b"][1] = 2.2;
00098             val["a"]["c"] = true;
00099             val["d"] = "string";
00100 
00101             JSValue result;
00102             to_js(result, val);
00103             return result;
00104         } HANDLE_JS_EXCEPTIONS;
00105     }
00106 
00107     static Handle<v8::Value>
00108     getLongLongInt(const Arguments & args)
00109     {
00110         try {
00111             // Json::Value val;
00112             // val["long_long"] = (2LL)<<33;
00113 
00114             JSValue result;
00115             to_js(result, (1LL)<<33);
00116             return result;
00117         } HANDLE_JS_EXCEPTIONS;
00118     }
00119 
00120     static Handle<v8::Value>
00121     getJSONLongLongInt(const Arguments & args)
00122     {
00123         try {
00124             Json::Value val;
00125             val["long_long"] = (1LL)<<33;
00126 
00127             JSValue result;
00128             to_js(result, val);
00129             return result;
00130         } HANDLE_JS_EXCEPTIONS;
00131     }
00132 };
00133 
00134 extern "C" void
00135 init(Handle<v8::Object> target)
00136 {
00137     registry.init(target, FromToModule);
00138 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator