RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
soa/js/testing/js_exception_passing_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 
00015 using namespace v8;
00016 using namespace std;
00017 using namespace Datacratic;
00018 using namespace Datacratic::JS;
00019 
00020 
00021 struct TestException {
00022 };
00023 
00024 const char * TestExceptionName = "TestException";
00025 const char * TestModule = "exc";
00026 
00027 struct TestExceptionJS
00028     : public JSWrapped2<TestException, TestExceptionJS,
00029                         TestExceptionName, TestModule> {
00030 
00031     static Persistent<v8::FunctionTemplate>
00032     Initialize()
00033     {
00034         Persistent<FunctionTemplate> t = Register(New, Setup);
00035         
00036         // Instance methods
00037         NODE_SET_PROTOTYPE_METHOD(t, "testMlException", testMlException);
00038         NODE_SET_PROTOTYPE_METHOD(t, "testMlException2", testMlException2);
00039         NODE_SET_PROTOTYPE_METHOD(t, "testStdException", testStdException);
00040         NODE_SET_PROTOTYPE_METHOD(t, "testPassThrough", testPassThrough);
00041         
00042         return t;
00043     }
00044 
00045     static Handle<Value>
00046     New(const Arguments & args)
00047     {
00048         try {
00049             new TestExceptionJS();
00050             return args.This();
00051         } HANDLE_JS_EXCEPTIONS;
00052     }
00053 
00054     static Handle<Value>
00055     testMlException(const Arguments & args)
00056     {
00057         try {
00058             try {
00059                 throw ML::Exception("hello");
00060             } catch (const ML::Exception & exc) {
00061                 //cerr << "exc is at " << &exc << endl;
00062                 throw;
00063             }
00064         } HANDLE_JS_EXCEPTIONS;
00065     }
00066 
00067     static Handle<Value>
00068     testMlException2(const Arguments & args)
00069     {
00070         try {
00071             throw ML::Exception("hello2");
00072         } HANDLE_JS_EXCEPTIONS;
00073     }
00074 
00075     static Handle<Value>
00076     testStdException(const Arguments & args)
00077     {
00078         try {
00079             try {
00080                 throw std::logic_error("bad medicine");
00081             } catch (const std::exception & exc) {
00082                 //cerr << "exc is at " << &exc << endl;
00083                 throw;
00084             }
00085         } HANDLE_JS_EXCEPTIONS;
00086     }
00087 
00088     static Handle<Value>
00089     testPassThrough(const Arguments & args)
00090     {
00091         try {
00092             v8::Local<v8::Function> fn(v8::Function::Cast(*args[0]));
00093             if (fn.IsEmpty())
00094                 throw ML::Exception("should have gotten throwing function in");
00095 
00096             // Call the function
00097             v8::Handle<v8::Value> result = fn->Call(args.This(), 0, 0);
00098             
00099             if (result.IsEmpty())
00100                 throw JSPassException();
00101 
00102             throw ML::Exception("test was supposed to have a function that threw");
00103         } HANDLE_JS_EXCEPTIONS;
00104     }
00105 
00106 
00107 };
00108 
00109 extern "C" void
00110 init(Handle<v8::Object> target)
00111 {
00112     registry.init(target, TestModule);
00113 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator