void JObjectWrap::set_jholder(JObject& jholder) { IOTJS_ASSERT(_jholder == NULL); IOTJS_ASSERT(jholder.IsObject()); JRawValueType raw_value = jholder.raw_value(); _jholder = new JObject(&raw_value, false); }
void OnRead(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) { TcpWrap* tcp_wrap = reinterpret_cast<TcpWrap*>(handle->data); IOTJS_ASSERT(tcp_wrap != NULL); // tcp handle JObject jtcp = tcp_wrap->jobject(); IOTJS_ASSERT(jtcp.IsObject()); // socket object JObject jsocket = jtcp.GetProperty("owner"); IOTJS_ASSERT(jsocket.IsObject()); // onread callback JObject jonread = jtcp.GetProperty("onread"); IOTJS_ASSERT(jonread.IsFunction()); JArgList jargs(4); jargs.Add(jsocket); jargs.Add(JVal::Number((int)nread)); jargs.Add(JVal::Bool(false)); if (nread <= 0) { if (buf->base != NULL) { ReleaseBuffer(buf->base); } if (nread < 0) { if (nread == UV__EOF) { jargs.Set(2, JVal::Bool(true)); } MakeCallback(jonread, JObject::Null(), jargs); } return; } JObject jbuffer(CreateBuffer(static_cast<size_t>(nread))); BufferWrap* buffer_wrap = BufferWrap::FromJBuffer(jbuffer); buffer_wrap->Copy(buf->base, nread); jargs.Add(jbuffer); MakeCallback(jonread, JObject::Null(), jargs); ReleaseBuffer(buf->base); }
JObjectWrap::JObjectWrap(JObject& jobject) : _jobject(NULL) { IOTJS_ASSERT(jobject.IsObject()); // This wrapper hold pointer to the javascript object but never increase // reference count. JRawValueType raw_value = jobject.raw_value(); _jobject = new JObject(&raw_value, false); // Set native pointer of the object to be this wrapper. // If the object is freed by GC, the wrapper instance should also be freed. _jobject->SetNative((uintptr_t)this, FreeObjectWrap); }
// Socket close result handler. static void AfterClose(uv_handle_t* handle) { HandleWrap* tcp_wrap = HandleWrap::FromHandle(handle); IOTJS_ASSERT(tcp_wrap != NULL); // tcp object. JObject jtcp = tcp_wrap->jobject(); IOTJS_ASSERT(jtcp.IsObject()); // callback function. JObject jcallback = jtcp.GetProperty("onclose"); if (jcallback.IsFunction()) { MakeCallback(jcallback, JObject::Null(), JArgList::Empty()); } }
// A client socket wants to connect to this server. // Parameters: // * uv_stream_t* handle - server handle // * int status - status code static void OnConnection(uv_stream_t* handle, int status) { // Server tcp wrapper. TcpWrap* tcp_wrap = reinterpret_cast<TcpWrap*>(handle->data); IOTJS_ASSERT(tcp_wrap->tcp_handle() == reinterpret_cast<uv_tcp_t*>(handle)); // Tcp object JObject jtcp = tcp_wrap->jobject(); IOTJS_ASSERT(jtcp.IsObject()); // `onconnection` callback. JObject jonconnection = jtcp.GetProperty("onconnection"); IOTJS_ASSERT(jonconnection.IsFunction()); // The callback takes two parameter // [0] status // [1] client tcp object JArgList args(2); args.Add(JVal::Number(status)); if (status == 0) { // Create client socket handle wrapper. JObject jcreate_tcp = jtcp.GetProperty("createTCP"); IOTJS_ASSERT(jcreate_tcp.IsFunction()); JObject jclient_tcp = jcreate_tcp.CallOk(JObject::Null(), JArgList::Empty()); IOTJS_ASSERT(jclient_tcp.IsObject()); TcpWrap* tcp_wrap = reinterpret_cast<TcpWrap*>(jclient_tcp.GetNative()); uv_stream_t* client_handle = reinterpret_cast<uv_stream_t*>(tcp_wrap->tcp_handle()); int err = uv_accept(handle, client_handle); if (err) { return; } args.Add(jclient_tcp); } MakeCallback(jonconnection, jtcp, args); }