RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
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 00015 using namespace v8; 00016 using namespace std; 00017 using namespace Datacratic; 00018 using namespace Datacratic::JS; 00019 00020 struct Base { 00021 virtual ~Base() 00022 { 00023 } 00024 00025 virtual std::string type() const 00026 { 00027 return "Base"; 00028 } 00029 00030 int number() const { return 27; } 00031 int number2() const { return 27; } 00032 }; 00033 00034 struct Derived : public Base { 00035 virtual std::string type() const { return "Derived"; } 00036 int number() const { return 37; } 00037 }; 00038 00039 struct ReDerived : public Derived { 00040 virtual std::string type() const { return "ReDerived"; } 00041 int number() const { return 47; } 00042 }; 00043 00044 const char * BaseName = "Base"; 00045 const char * Module = "inheritance"; 00046 00047 struct BaseJS : public JSWrapped2<Base, BaseJS, BaseName, Module> { 00048 00049 BaseJS() 00050 { 00051 } 00052 00053 BaseJS(const v8::Arguments & args) 00054 { 00055 wrap(args.This(), new Base()); 00056 } 00057 00058 static Persistent<v8::FunctionTemplate> 00059 Initialize() 00060 { 00061 Persistent<FunctionTemplate> t = Register(New, Setup); 00062 00063 // Instance methods 00064 NODE_SET_PROTOTYPE_METHOD(t, "type", type); 00065 NODE_SET_PROTOTYPE_METHOD(t, "number", number); 00066 NODE_SET_PROTOTYPE_METHOD(t, "number2", number2); 00067 NODE_SET_PROTOTYPE_METHOD(t, "otherType", otherType); 00068 00069 return t; 00070 } 00071 00072 static Handle<Value> 00073 New(const Arguments & args) 00074 { 00075 try { 00076 new BaseJS(args); 00077 return args.This(); 00078 } HANDLE_JS_EXCEPTIONS; 00079 } 00080 00081 static Handle<Value> 00082 type(const Arguments & args) 00083 { 00084 HandleScope scope; 00085 try { 00086 return v8::String::NewSymbol(getShared(args)->type().c_str()); 00087 } HANDLE_JS_EXCEPTIONS; 00088 } 00089 00090 static Handle<Value> 00091 number(const Arguments & args) 00092 { 00093 HandleScope scope; 00094 try { 00095 return v8::Integer::New(getShared(args)->number()); 00096 } HANDLE_JS_EXCEPTIONS; 00097 } 00098 00099 static Handle<Value> 00100 number2(const Arguments & args) 00101 { 00102 HandleScope scope; 00103 try { 00104 return v8::Integer::New(getShared(args)->number2()); 00105 } HANDLE_JS_EXCEPTIONS; 00106 } 00107 00108 static Handle<Value> 00109 otherType(const Arguments & args) 00110 { 00111 HandleScope scope; 00112 try { 00113 Handle<Object> obj = args[0]->ToObject(); 00114 if (obj.IsEmpty()) 00115 throw ML::Exception("arg 0 wasn't an object"); 00116 Base * other = BaseJS::getShared(obj); 00117 return v8::String::New(other->type().c_str()); 00118 } HANDLE_JS_EXCEPTIONS; 00119 } 00120 }; 00121 00122 const char * DerivedName = "Derived"; 00123 00124 struct DerivedJS 00125 : public JSWrapped3<Derived, DerivedJS, BaseJS, DerivedName, Module> { 00126 00127 DerivedJS() 00128 { 00129 } 00130 00131 DerivedJS(const v8::Arguments & args) 00132 { 00133 wrap(args.This(), new Derived()); 00134 } 00135 00136 static Persistent<v8::FunctionTemplate> 00137 Initialize() 00138 { 00139 Persistent<FunctionTemplate> t = Register(New, Setup); 00140 00141 // Instance methods 00142 NODE_SET_PROTOTYPE_METHOD(t, "number", number); 00143 NODE_SET_PROTOTYPE_METHOD(t, "otherTypeDerived", otherTypeDerived); 00144 00145 return t; 00146 } 00147 00148 static Handle<Value> 00149 New(const Arguments & args) 00150 { 00151 try { 00152 new DerivedJS(args); 00153 return args.This(); 00154 } HANDLE_JS_EXCEPTIONS; 00155 } 00156 00157 static Handle<Value> 00158 number(const Arguments & args) 00159 { 00160 HandleScope scope; 00161 try { 00162 return v8::Integer::New(getShared(args)->number()); 00163 } HANDLE_JS_EXCEPTIONS; 00164 } 00165 00166 static Handle<Value> 00167 otherTypeDerived(const Arguments & args) 00168 { 00169 HandleScope scope; 00170 try { 00171 Handle<Object> obj = args[0]->ToObject(); 00172 if (obj.IsEmpty()) 00173 throw ML::Exception("arg 0 wasn't an object"); 00174 Derived * other = DerivedJS::getShared(obj); 00175 return v8::String::New(other->type().c_str()); 00176 } HANDLE_JS_EXCEPTIONS; 00177 } 00178 }; 00179 00180 const char * ReDerivedName = "ReDerived"; 00181 00182 struct ReDerivedJS 00183 : public JSWrapped3<ReDerived, ReDerivedJS, DerivedJS, ReDerivedName, 00184 Module> { 00185 00186 ReDerivedJS(const v8::Arguments & args) 00187 { 00188 wrap(args.This(), new ReDerived()); 00189 } 00190 00191 static Persistent<v8::FunctionTemplate> 00192 Initialize() 00193 { 00194 Persistent<FunctionTemplate> t = Register(New, Setup); 00195 00196 // Instance methods 00197 NODE_SET_PROTOTYPE_METHOD(t, "number", number); 00198 00199 return t; 00200 } 00201 00202 static Handle<Value> 00203 New(const Arguments & args) 00204 { 00205 try { 00206 new ReDerivedJS(args); 00207 return args.This(); 00208 } HANDLE_JS_EXCEPTIONS; 00209 } 00210 00211 static Handle<Value> 00212 number(const Arguments & args) 00213 { 00214 HandleScope scope; 00215 try { 00216 return v8::Integer::New(getShared(args)->number()); 00217 } HANDLE_JS_EXCEPTIONS; 00218 } 00219 }; 00220 00221 struct Base2 { 00222 virtual ~Base2() 00223 { 00224 } 00225 00226 virtual std::string type() const 00227 { 00228 return "Base2"; 00229 } 00230 00231 int number() const { return 27; } 00232 int number2() const { return 27; } 00233 }; 00234 00235 const char * Base2Name = "Base2"; 00236 00237 struct Base2JS : public JSWrapped2<Base2, Base2JS, Base2Name, Module> { 00238 00239 Base2JS() 00240 { 00241 } 00242 00243 Base2JS(const v8::Arguments & args) 00244 { 00245 wrap(args.This(), new Base2()); 00246 } 00247 00248 static Persistent<v8::FunctionTemplate> 00249 Initialize() 00250 { 00251 Persistent<FunctionTemplate> t = Register(New, Setup); 00252 00253 // Instance methods 00254 NODE_SET_PROTOTYPE_METHOD(t, "type", type); 00255 NODE_SET_PROTOTYPE_METHOD(t, "number", number); 00256 NODE_SET_PROTOTYPE_METHOD(t, "number2", number2); 00257 00258 return t; 00259 } 00260 00261 static Handle<Value> 00262 New(const Arguments & args) 00263 { 00264 try { 00265 new BaseJS(args); 00266 return args.This(); 00267 } HANDLE_JS_EXCEPTIONS; 00268 } 00269 00270 static Handle<Value> 00271 type(const Arguments & args) 00272 { 00273 HandleScope scope; 00274 try { 00275 return v8::String::NewSymbol(getShared(args)->type().c_str()); 00276 } HANDLE_JS_EXCEPTIONS; 00277 } 00278 00279 static Handle<Value> 00280 number(const Arguments & args) 00281 { 00282 HandleScope scope; 00283 try { 00284 return v8::Integer::New(getShared(args)->number()); 00285 } HANDLE_JS_EXCEPTIONS; 00286 } 00287 00288 static Handle<Value> 00289 number2(const Arguments & args) 00290 { 00291 HandleScope scope; 00292 try { 00293 return v8::Integer::New(getShared(args)->number2()); 00294 } HANDLE_JS_EXCEPTIONS; 00295 } 00296 }; 00297 00298 extern "C" void 00299 init(Handle<v8::Object> target) 00300 { 00301 registry.init(target, Module); 00302 }