soa/sigslot/slot_js.cc
00001 /* slot_js.cc 00002 Jeremy Barnes, 16 November 2010 00003 Copyright (c) 2010 Datacratic. All rights reserved. 00004 00005 JS interface to slots. 00006 */ 00007 00008 #include "slot_js.h" 00009 #include "soa/sigslot/slot.h" 00010 00011 #include "node.h" 00012 00013 #include "soa/js/js_value.h" 00014 #include "soa/js/js_utils.h" 00015 #include "soa/js/js_wrapped.h" 00016 00017 #include "jml/utils/smart_ptr_utils.h" 00018 00019 00020 using namespace std; 00021 using namespace ML; 00022 00023 00024 namespace Datacratic { 00025 namespace JS { 00026 00027 00028 /*****************************************************************************/ 00029 /* SLOTJS */ 00030 /*****************************************************************************/ 00031 00032 const char * SlotName = "Slot"; 00033 00034 struct SlotJS : public JSWrapped2<Slot, SlotJS, SlotName, sigslotModule> { 00035 00036 SlotJS(v8::Handle<v8::Object> This, 00037 const std::shared_ptr<Slot> & slot 00038 = std::shared_ptr<Slot>()) 00039 { 00040 wrap(This, slot); 00041 } 00042 00043 SlotJS(v8::Handle<v8::Object> This, 00044 const Slot & slot) 00045 { 00046 wrap(This, make_std_sp(new Slot(slot))); 00047 } 00048 00049 static v8::Handle<v8::Value> 00050 New(const v8::Arguments & args) 00051 { 00052 try { 00053 new SlotJS(args.This()); 00054 return args.This(); 00055 } HANDLE_JS_EXCEPTIONS; 00056 } 00057 00058 static v8::Handle<v8::Value> 00059 call(const v8::Arguments & args) 00060 { 00061 try { 00062 return getShared(args)->call(args); 00063 } HANDLE_JS_EXCEPTIONS; 00064 } 00065 00066 static void Initialize() 00067 { 00068 using namespace v8; 00069 v8::Persistent<v8::FunctionTemplate> t = Register(New); 00070 NODE_SET_PROTOTYPE_METHOD(t, "toString", toString); 00071 NODE_SET_PROTOTYPE_METHOD(t, "inspect", toString); 00072 NODE_SET_PROTOTYPE_METHOD(t, "call", call); 00073 t->InstanceTemplate()->SetCallAsFunctionHandler(call); 00074 } 00075 00076 static v8::Handle<v8::Value> 00077 toString(const v8::Arguments & args) 00078 { 00079 try { 00080 return JS::toJS("[Slot " + getShared(args)->print() + "]"); 00081 } HANDLE_JS_EXCEPTIONS; 00082 } 00083 }; 00084 00085 00086 Slot * from_js(const JSValue & val, Slot **) 00087 { 00088 return SlotJS::fromJS(val).get(); 00089 } 00090 00091 std::shared_ptr<Slot> 00092 from_js(const JSValue & val, std::shared_ptr<Slot>*) 00093 { 00094 return SlotJS::fromJS(val); 00095 } 00096 00097 void to_js(JS::JSValue & value, const std::shared_ptr<Slot> & slot) 00098 { 00099 if (slot->isEmpty()) 00100 value = v8::Null(); 00101 else value = SlotJS::toJS(slot); 00102 } 00103 00104 void to_js(JS::JSValue & value, const Slot & slot) 00105 { 00106 if (slot.isEmpty()) 00107 value = v8::Null(); 00108 else to_js(value, make_std_sp(new Slot(slot))); 00109 } 00110 00111 const char * const sigslotModule = "sigslot"; 00112 00113 } // namespace JS 00114 00115 v8::Local<v8::Function> 00116 Slot:: 00117 toJsFunction() const 00118 { 00119 v8::HandleScope scope; 00120 00121 std::shared_ptr<Slot> s(new Slot(*this)); 00122 v8::Local<v8::Value> res = JS::SlotJS::toJS(s); 00123 00124 v8::Local<v8::Function> cast = v8::Function::Cast(*res); 00125 00126 if (cast.IsEmpty() || cast->IsUndefined() || cast->IsNull()) 00127 throw Exception("Slot is not a function"); 00128 00129 return cast; 00130 } 00131 00132 } // namespace Datacratic