Handle<Value> Zipper::addFile(const Arguments& args) { HandleScope scope; if (args.Length() < 3) return ThrowException(Exception::TypeError( String::New("requires three arguments, the path of a file, a filename and a callback"))); // first arg must be path if(!args[0]->IsString()) return ThrowException(Exception::TypeError( String::New("first argument must be a file path to add to the zip"))); // second arg must be name if(!args[1]->IsString()) return ThrowException(Exception::TypeError( String::New("second argument must be a file name to add to the zip"))); // last arg must be function callback if (!args[args.Length()-1]->IsFunction()) return ThrowException(Exception::TypeError( String::New("last argument must be a callback function"))); std::string path = TOSTR(args[0]); std::string name = TOSTR(args[1]); Zipper* zf = ObjectWrap::Unwrap<Zipper>(args.This()); closure_t *closure = new closure_t(); // libzip is not threadsafe so we cannot use the zf->archive_ // instead we open a new zip archive for each thread struct zip *za; int err; char errstr[1024]; if ((za=zip_open(zf->file_name_.c_str() , ZIP_CREATE, &err)) == NULL) { zip_error_to_str(errstr, sizeof(errstr), err, errno); std::stringstream s; s << "cannot open file: " << zf->file_name_ << " error: " << errstr << "\n"; zip_close(za); return ThrowException(Exception::Error(String::New(s.str().c_str()))); } closure->zf = zf; closure->za = za; closure->error = false; closure->path = path; closure->name = name; closure->cb = Persistent<Function>::New(Handle<Function>::Cast(args[args.Length()-1])); eio_custom(EIO_AddFile, EIO_PRI_DEFAULT, EIO_AfterAddFile, closure); ev_ref(EV_DEFAULT_UC); zf->Ref(); return Undefined(); }
/* stub implementation of uv_getaddrinfo */ int uv_getaddrinfo(uv_loop_t* loop, uv_getaddrinfo_t* handle, uv_getaddrinfo_cb cb, const char* hostname, const char* service, const struct addrinfo* hints) { eio_req* req; uv_eio_init(loop); if (handle == NULL || cb == NULL || (hostname == NULL && service == NULL)) { uv__set_artificial_error(loop, UV_EINVAL); return -1; } uv__req_init(loop, (uv_req_t*)handle); handle->type = UV_GETADDRINFO; handle->loop = loop; handle->cb = cb; /* TODO don't alloc so much. */ if (hints) { handle->hints = malloc(sizeof(struct addrinfo)); memcpy(handle->hints, hints, sizeof(struct addrinfo)); } else { handle->hints = NULL; } /* TODO security! check lengths, check return values. */ handle->hostname = hostname ? strdup(hostname) : NULL; handle->service = service ? strdup(service) : NULL; handle->res = NULL; handle->retcode = 0; /* TODO check handle->hostname == NULL */ /* TODO check handle->service == NULL */ uv_ref(loop); req = eio_custom(getaddrinfo_thread_proc, EIO_PRI_DEFAULT, uv_getaddrinfo_done, handle, &loop->uv_eio_channel); assert(req); assert(req->data == handle); return 0; }
/* stub implementation of uv_getaddrinfo */ int uv_getaddrinfo(uv_loop_t* loop, uv_getaddrinfo_t* handle, uv_getaddrinfo_cb cb, const char* hostname, const char* service, const struct addrinfo* hints) { eio_req* req; uv_eio_init(loop); if (handle == NULL || cb == NULL || (hostname == NULL && service == NULL)) { uv_err_new_artificial(loop, UV_EINVAL); return -1; } memset(handle, 0, sizeof(uv_getaddrinfo_t)); /* TODO don't alloc so much. */ if (hints) { handle->hints = malloc(sizeof(struct addrinfo)); memcpy(&handle->hints, hints, sizeof(struct addrinfo)); } /* TODO security! check lengths, check return values. */ handle->loop = loop; handle->cb = cb; handle->hostname = hostname ? strdup(hostname) : NULL; handle->service = service ? strdup(service) : NULL; /* TODO check handle->hostname == NULL */ /* TODO check handle->service == NULL */ uv_ref(loop); req = eio_custom(getaddrinfo_thread_proc, EIO_PRI_DEFAULT, uv_getaddrinfo_done, handle); assert(req); assert(req->data == handle); return 0; }
/* zipfile.read(buffer, pos, len, cb) -> cb(bytesRead, error) */ Handle<Value> ZipFile::Read(const Arguments& args) { ZipFile* zf = ObjectWrap::Unwrap<ZipFile>(args.This()); if (!Buffer::HasInstance(args[0])) { return ThrowException(Exception::Error( String::New("First argument needs to be a buffer"))); } Local<Object> buffer_obj = args[0]->ToObject(); char *buffer_data = Buffer::Data(buffer_obj); size_t buffer_length = Buffer::Length(buffer_obj); zip_uint64_t off = args[1]->Int32Value(); if (off >= buffer_length) { return ThrowException(Exception::Error( String::New("Offset is out of bounds"))); } zip_uint64_t len = args[2]->Int32Value(); if (off + len > buffer_length) { return ThrowException(Exception::Error( String::New("Length is extends beyond buffer"))); } Local<Value> cb = args[3]; if (!cb->IsFunction()) return ThrowException(Exception::Error( String::New("Fourth argument should be a callback function."))); read_closure_t *closure = new read_closure_t(); closure->zf = zf; closure->read = 0; closure->data = buffer_data+off; closure->len = len; closure->cb = Persistent<Function>::New(Handle<Function>::Cast(args[3])); eio_custom(EIO_Read, EIO_PRI_DEFAULT, EIO_AfterRead, closure); zf->Ref(); ev_ref(EV_DEFAULT_UC); return Undefined(); }
int uv_queue_work(uv_loop_t* loop, uv_work_t* req, uv_work_cb work_cb, uv_after_work_cb after_work_cb) { void* data = req->data; uv_eio_init(loop); uv__req_init((uv_req_t*) req); uv_ref(loop); req->loop = loop; req->data = data; req->work_cb = work_cb; req->after_work_cb = after_work_cb; req->eio = eio_custom(uv__work, EIO_PRI_DEFAULT, uv__after_work, req); if (!req->eio) { uv_err_new(loop, ENOMEM); return -1; } return 0; }
// db, statement, callback static v8::Handle<Value> ExecuteStatement(const Arguments& args) { v8::HandleScope scope; REQ_DBCONN_ARG(0); REQ_STR_ARG(1, stmt); REQ_FUN_ARG(2, cb); v8::String::Utf8Value statement(stmt); if(dbcmd(dbconn, *statement) == FAIL){ return v8::ThrowException(v8::Exception::Error(v8::String::New("FreeTDS could allocate enough memory for the statement"))); } data_callback_t *callbackData = new data_callback_t(); callbackData->dbconn = dbconn; callbackData->callback = Persistent<Function>::New(cb); if(dbsqlsend(dbconn) == FAIL){ return v8::ThrowException(v8::Exception::Error(v8::String::New("FreeTDS could not send the statement"))); } eio_custom(waitForDataResponse, EIO_PRI_DEFAULT, onDataResponse, callbackData); return v8::Undefined(); }
int uv_getaddrinfo(uv_loop_t* loop, uv_getaddrinfo_t* req, uv_getaddrinfo_cb cb, const char* hostname, const char* service, const struct addrinfo* hints) { size_t hostname_len; size_t service_len; size_t hints_len; eio_req* req_; size_t len; char* buf; if (req == NULL || cb == NULL || (hostname == NULL && service == NULL)) return uv__set_artificial_error(loop, UV_EINVAL); uv_eio_init(loop); hostname_len = hostname ? strlen(hostname) + 1 : 0; service_len = service ? strlen(service) + 1 : 0; hints_len = hints ? sizeof(*hints) : 0; buf = malloc(hostname_len + service_len + hints_len); if (buf == NULL) return uv__set_artificial_error(loop, UV_ENOMEM); uv__req_init(loop, req, UV_GETADDRINFO); req->loop = loop; req->cb = cb; req->res = NULL; req->hints = NULL; req->service = NULL; req->hostname = NULL; req->retcode = 0; /* order matters, see uv_getaddrinfo_done() */ len = 0; if (hints) { req->hints = memcpy(buf + len, hints, sizeof(*hints)); len += sizeof(*hints); } if (service) { req->service = memcpy(buf + len, service, service_len); len += service_len; } if (hostname) { req->hostname = memcpy(buf + len, hostname, hostname_len); len += hostname_len; } req_ = eio_custom(getaddrinfo_thread_proc, EIO_PRI_DEFAULT, uv_getaddrinfo_done, req, &loop->uv_eio_channel); if (req_) return 0; free(buf); return uv__set_artificial_error(loop, UV_ENOMEM); }