RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 00006 #include <signal.h> 00007 #include "soa/js/js_wrapped.h" 00008 #include "soa/js/js_utils.h" 00009 #include "soa/js/js_registry.h" 00010 #include "v8.h" 00011 #include "jml/compiler/compiler.h" 00012 #include "jml/utils/smart_ptr_utils.h" 00013 #include "soa/jsoncpp/json.h" 00014 00015 using namespace v8; 00016 using namespace std; 00017 using namespace Datacratic; 00018 using namespace Datacratic::JS; 00019 00020 00021 00022 struct ArityTestClass { 00023 virtual ~ArityTestClass() 00024 { 00025 } 00026 00027 int method(int arg = 10) 00028 { 00029 return arg; 00030 } 00031 00032 int constMethod(int arg = 6) const 00033 { 00034 return arg; 00035 } 00036 00037 std::pair<string, int> 00038 twoArgMethod(string arg1 = "hello", int arg2 = 123) 00039 { 00040 return std::make_pair(arg1, arg2); 00041 } 00042 }; 00043 00044 const char * ArityTestClassName = "ArityTestClass"; 00045 const char * ArityTestModule = "ft"; 00046 00047 struct ArityTestClassJS 00048 : public JSWrapped2<ArityTestClass, ArityTestClassJS, ArityTestClassName, ArityTestModule> { 00049 00050 ArityTestClassJS(v8::Handle<v8::Object> This, 00051 const std::shared_ptr<ArityTestClass> & fromto 00052 = std::shared_ptr<ArityTestClass>()) 00053 { 00054 HandleScope scope; 00055 wrap(This, fromto); 00056 } 00057 00058 static Persistent<v8::FunctionTemplate> 00059 Initialize() 00060 { 00061 Persistent<FunctionTemplate> t = Register(New, Setup); 00062 00063 registerMemberFn(&ArityTestClass::method, "method", 10); 00064 registerMemberFn(&ArityTestClass::constMethod, "constMethod", 6); 00065 registerMemberFn(&ArityTestClass::twoArgMethod, "twoArgMethod", "hello", 123); 00066 00067 #if 0 00068 auto m = &ArityTestClass::method; 00069 ArityTestClass * p = 0; 00070 int res = ((*p).*(m)) (2); 00071 cerr << "res = " << res << endl; 00072 #endif 00073 00074 return t; 00075 } 00076 00077 static Handle<Value> 00078 New(const Arguments & args) 00079 { 00080 try { 00081 new ArityTestClassJS(args.This(), ML::make_std_sp(new ArityTestClass())); 00082 return args.This(); 00083 } HANDLE_JS_EXCEPTIONS; 00084 } 00085 00086 static Handle<v8::Value> 00087 roundTrip(const Arguments & args) 00088 { 00089 try { 00090 Json::Value val = getArg(args, 0, "arg"); 00091 JSValue result; 00092 to_js(result, val); 00093 return result; 00094 } HANDLE_JS_EXCEPTIONS; 00095 } 00096 00097 static Handle<v8::Value> 00098 getJSON1(const Arguments & args) 00099 { 00100 try { 00101 Json::Value val; 00102 val["a"] = 1; 00103 JSValue result; 00104 to_js(result, val); 00105 return result; 00106 } HANDLE_JS_EXCEPTIONS; 00107 } 00108 00109 static Handle<v8::Value> 00110 getJSON2(const Arguments & args) 00111 { 00112 try { 00113 Json::Value val; 00114 val["a"]["b"][0u] = 1; 00115 val["a"]["b"][1] = 2.2; 00116 val["a"]["c"] = true; 00117 val["d"] = "string"; 00118 00119 JSValue result; 00120 to_js(result, val); 00121 return result; 00122 } HANDLE_JS_EXCEPTIONS; 00123 } 00124 }; 00125 00126 extern "C" void 00127 init(Handle<v8::Object> target) 00128 { 00129 registry.init(target, ArityTestModule); 00130 } 00131 00132 int main(int argc, char ** argv) 00133 { 00134 }