std::string logArgs2String(const Args &args) { std::string message; for (int i = 0; i < args.Length(); i++) { ::v8::HandleScope scope; ::v8::String::Utf8Value str(args[i]); message += *str; } return message; }
void Bindings::global(const Args& args) { Isolate* isolate(args.GetIsolate()); HandleScope scope(isolate); if (args.Length() != 3) { throw std::runtime_error("Wrong number of arguments to global"); } if (stageFactory) { throw std::runtime_error("Multiple global initializations attempted"); } std::size_t i(0); const auto& pathsArg(args[i++]); const auto& cacheSizeArg(args[i++]); const auto& arbiterArg(args[i++]); std::string errMsg; if (!pathsArg->IsArray()) errMsg += "\t'paths' must be an array"; if (!cacheSizeArg->IsNumber()) errMsg += "\t'cacheSize' must be a number"; if (!arbiterArg->IsString()) errMsg += "\t'arbiter' must be a string"; paths = parsePathList(isolate, pathsArg); const std::size_t cacheSize(cacheSizeArg->NumberValue()); cache = entwine::makeUnique<entwine::Cache>(cacheSize); const std::string arbiterString( *v8::String::Utf8Value(arbiterArg->ToString())); outerScope.getArbiter(entwine::parse(arbiterString)); signal(SIGSEGV, handler); curl_global_init(CURL_GLOBAL_ALL); stageFactory = entwine::makeUnique<pdal::StageFactory>(); }
void Bindings::create(const Args& args) { Isolate* isolate(args.GetIsolate()); HandleScope scope(isolate); Bindings* obj = ObjectWrap::Unwrap<Bindings>(args.Holder()); if (args.Length() != 2) { throw std::runtime_error("Wrong number of arguments to create"); } std::size_t i(0); const auto& nameArg (args[i++]); const auto& cbArg (args[i++]); std::string errMsg; if (!nameArg->IsString()) errMsg += "\t'name' must be a string"; if (!cbArg->IsFunction()) throw std::runtime_error("Invalid create CB"); UniquePersistent<Function> callback(isolate, Local<Function>::Cast(cbArg)); if (errMsg.size()) { std::cout << "Client error: " << errMsg << std::endl; Status status(400, errMsg); const unsigned argc = 1; Local<Value> argv[argc] = { status.toObject(isolate) }; Local<Function> local(Local<Function>::New(isolate, callback)); local->Call(isolate->GetCurrentContext()->Global(), argc, argv); callback.Reset(); return; } const std::string name(*v8::String::Utf8Value(nameArg->ToString())); // Store everything we'll need to perform initialization. uv_work_t* req(new uv_work_t); req->data = new CreateData( obj->m_session, name, paths, outerScope, cache, std::move(callback)); uv_queue_work( uv_default_loop(), req, (uv_work_cb)([](uv_work_t *req)->void { CreateData* createData(static_cast<CreateData*>(req->data)); createData->safe([createData]()->void { if (!createData->session->initialize( createData->name, createData->paths, createData->outerScope, createData->cache)) { createData->status.set(404, "Not found"); } }); }), (uv_after_work_cb)([](uv_work_t* req, int status)->void { Isolate* isolate(Isolate::GetCurrent()); HandleScope scope(isolate); CreateData* createData(static_cast<CreateData*>(req->data)); const unsigned argc = 1; Local<Value> argv[argc] = { createData->status.toObject(isolate) }; Local<Function> local(Local<Function>::New( isolate, createData->callback)); local->Call(isolate->GetCurrentContext()->Global(), argc, argv); delete createData; delete req; }) ); }