void PhysObject::DrawBounds() { JObjectIterator it( this ); ++it; while (it) { JObject* pObj = *it; if (!pObj->IsDrawBounds()) { pObj->DrawBounds(); } ++it; } } // PhysObject::DrawBounds
int JLuaServer::FnCall( lua_State* pLua ) { if (lua_gettop( pLua ) < 2 || lua_gettop( pLua ) > 3) { rlog.err( "LUA: Incorrect command usage: <call>. " "Function takes 2 to 3 arguments (object|objectName, funcName, [tag]), but %d is provided", lua_gettop( pLua ) ); lua_settop( pLua, 0 ); return 0; } JObject* pObj = NULL; const char* pPath = lua_tostring( pLua, 1 ); if (pPath) { JLuaThread* pThread = reinterpret_cast<JLuaThread*>( pLua->userdata ); assert( pThread->m_pLua == pLua ); JObject* pRootObj = pThread->m_pRootObj; pObj = g_pObjectServer->FindObject( pPath, NULL, pRootObj ); if (!pObj) { rlog.warn( "LUA: Method <call> refers to non-existing object: %s", pPath ); return NULL; } } else { pObj = reinterpret_cast<JObject*>( lua_touserdata( pLua, 1 ) ); if (!pObj) { rlog.warn( "LUA: Trying to call <call> for the nil object" ); return NULL; } } const char* pMethodName = lua_tostring( pLua, 2 ); int tag = -1; if (lua_isnumber( pLua, 3 )) { tag = (int)lua_tonumber( pLua, 3 ); } bool bRes = pObj->CallMethod( pMethodName, tag ); if (!bRes) { rlog.err( "LUA: Could not call method '%s' for object '%s' of type '%s'.", pMethodName, pObj->GetName(), pObj->ClassName() ); } return 0; } // JLuaServer::FnCall
// 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()); } }
int JLuaServer::FnGet( lua_State* pLua ) { if (lua_gettop( pLua ) < 2 || lua_gettop( pLua ) > 3) { rlog.err( "LUA: Incorrect command usage: <get>. " "Function takes 2 or 3 arguments (<objectRef>|<objectName>, <propName>, [tag]), but %d is provided", lua_gettop( pLua ) ); lua_settop( pLua, 0 ); lua_pushnil( pLua ); return 1; } JObject* pObj = NULL; const char* pPath = lua_tostring( pLua, 1 ); if (pPath) { JLuaThread* pThread = reinterpret_cast<JLuaThread*>( pLua->userdata ); assert( pThread->m_pLua == pLua ); JObject* pRootObj = pThread->m_pRootObj; pObj = g_pObjectServer->FindObject( pPath, NULL, pRootObj ); } else { pObj = reinterpret_cast<JObject*>( lua_touserdata( pLua, 1 ) ); } const char* pPropName = lua_tostring( pLua, 2 ); int tag = -1; if (lua_isnumber( pLua, 3 )) { tag = (int)lua_tonumber( pLua, 3 ); } if (!pObj) { rlog.warn( "LUA: Trying to call <get> for the nil object. Nil is returned." ); lua_pushnil( pLua ); return 1; } static JString val; bool bRes = pObj->GetProperty( pPropName, val, tag ); if (!bRes) { rlog.err( "LUA: Could not get property '%s' for object '%s' of type '%s'.", pPropName, pObj->GetName(), pObj->ClassName() ); } lua_pushstring( pLua, val.c_str() ); return 1; } // JLuaServer::FnGet
JHANDLER_FUNCTION(Buffer, handler) { IOTJS_ASSERT(handler.GetThis()->IsObject()); IOTJS_ASSERT(handler.GetArgLength() == 2); IOTJS_ASSERT(handler.GetArg(0)->IsObject()); IOTJS_ASSERT(handler.GetArg(1)->IsNumber()); int length = handler.GetArg(1)->GetInt32(); JObject* jbuffer = handler.GetArg(0); JObject* jbuiltin = handler.GetThis(); BufferWrap* buffer_wrap = new BufferWrap(*jbuffer, *jbuiltin, length); IOTJS_ASSERT(buffer_wrap == (BufferWrap*)(jbuiltin->GetNative())); IOTJS_ASSERT(buffer_wrap->buffer() != NULL); return true; }
int JPoint::compareTo(const JObject& s) const { if (className() != s.className()) return JObject::compareTo(s); int result = y-((JPoint*)&s)->y; if (!result) result = x-((JPoint*)&s)->x; return result; }
JObject MakeStatObject(uv_stat_t* statbuf) { #define X(name) \ JObject name((int)statbuf->st_##name); \ X(dev) X(mode) X(nlink) X(uid) X(gid) X(rdev) #undef X #define X(name) \ JObject name((double)statbuf->st_##name); \ X(blksize) X(ino) X(size) X(blocks) #undef X Module* module = GetBuiltinModule(MODULE_FS); JObject* fs = module->module; JObject createStat = fs->GetProperty("createStat"); JArgList args(10); args.Add(dev); args.Add(mode); args.Add(nlink); args.Add(uid); args.Add(gid); args.Add(rdev); args.Add(blksize); args.Add(ino); args.Add(size); args.Add(blocks); JResult jstat_res(createStat.Call(JObject::Null(), args)); IOTJS_ASSERT(jstat_res.IsOk()); return jstat_res.value(); }
JObject* InitFs() { Module* module = GetBuiltinModule(MODULE_FS); JObject* fs = module->module; if (fs == NULL) { fs = new JObject(); fs->SetMethod("close", Close); fs->SetMethod("open", Open); fs->SetMethod("read", Read); fs->SetMethod("write", Write); fs->SetMethod("stat", Stat); module->module = fs; } return fs; }
int JLuaServer::FnSetParent( lua_State* pLua ) { if (lua_gettop( pLua ) != 2) { rlog.err( "LUA: Incorrect command usage: <setparent>. " "Function takes 2 arguments, but %d is provided", lua_gettop( pLua ) ); lua_settop( pLua, 0 ); lua_pushnil( pLua ); return 1; } JObject* pParent = reinterpret_cast<JObject*>( lua_touserdata( pLua, 2 ) ); JObject* pObj = reinterpret_cast<JObject*>( lua_touserdata( pLua, 1 ) ); if (!pObj) { lua_pushnil( pLua ); return 0; } // set new parent for the object JObject* pOldParent = pObj->GetParent(); if (pParent) { pParent->AddChild( pObj ); } if (pOldParent) { pOldParent->RemoveChild( pObj ); } pObj->SetParent( pParent ); return 0; } // JLuaServer::FnSetParent
int JLuaServer::FnChildren( lua_State* pLua ) { if (lua_gettop( pLua ) != 1) { rlog.err( "LUA: Incorrect command usage: <children>. " "Function takes 1 argument (<objectRef>|<objectName>), but %d is provided", lua_gettop( pLua ) ); lua_settop( pLua, 0 ); lua_pushnil( pLua ); return 1; } JObject* pObj = NULL; const char* pPath = lua_tostring( pLua, -1 ); if (pPath) { JLuaThread* pThread = reinterpret_cast<JLuaThread*>( pLua->userdata ); assert( pThread->m_pLua == pLua ); JObject* pRootObj = pThread->m_pRootObj; pObj = g_pObjectServer->FindObject( pPath, NULL, pRootObj ); } else { pObj = reinterpret_cast<JObject*>( lua_touserdata( pLua, -1 ) ); } if (!pObj) { lua_pushnil( pLua ); return 1; } // return table with children int nCh = pObj->GetNChildren(); lua_createtable( pLua, nCh, 0 ); lua_newtable( pLua ); for (int i = 0; i < nCh; i++) { // key lua_pushnumber( pLua, i + 1 ); // value (child pointer) lua_pushlightuserdata( pLua, pObj->GetChild( i ) ); lua_settable( pLua, -3 ); } return 1; } // JLuaServer::FnChildren
void JObject::SetProperty(const char* name, const JObject& val) { IOTJS_ASSERT(IsObject()); JRawValueType v = val.raw_value(); bool is_ok = jerry_api_set_object_field_value( _obj_val.v_object, reinterpret_cast<const jerry_api_char_t*>(name), &v); IOTJS_ASSERT(is_ok); }
int JThread::compareTo(const JObject& s) const { if (className() != s.className()) return JObject::compareTo(s); return id-((JThread*)&s)->id; }
JObject MakeStatObject(uv_stat_t* statbuf) { Module* module = GetBuiltinModule(MODULE_FS); IOTJS_ASSERT(module != NULL); JObject* fs = module->module; IOTJS_ASSERT(fs != NULL); JObject createStat = fs->GetProperty("_createStat"); IOTJS_ASSERT(createStat.IsFunction()); JObject jstat; #define X(statobj, name) \ JObject name((int32_t)statbuf->st_##name); \ statobj.SetProperty(#name, name); \ X(jstat, dev) X(jstat, mode) X(jstat, nlink) X(jstat, uid) X(jstat, gid) X(jstat, rdev) #undef X #define X(statobj, name) \ JObject name((double)statbuf->st_##name); \ statobj.SetProperty(#name, name); \ X(jstat, blksize) X(jstat, ino) X(jstat, size) X(jstat, blocks) #undef X JArgList jargs(1); jargs.Add(jstat); JResult jstat_res(createStat.Call(JObject::Null(), jargs)); IOTJS_ASSERT(jstat_res.IsOk()); return jstat_res.value(); }
static void After(uv_fs_t* req) { FsReqWrap* req_wrap = static_cast<FsReqWrap*>(req->data); IOTJS_ASSERT(req_wrap != NULL); IOTJS_ASSERT(req_wrap->data() == req); JObject cb = req_wrap->jcallback(); IOTJS_ASSERT(cb.IsFunction()); JArgList jarg(2); if (req->result < 0) { JObject jerror(CreateUVException(req->result, "open")); jarg.Add(jerror); } else { jarg.Add(JObject::Null()); switch (req->fs_type) { case UV_FS_CLOSE: { break; } case UV_FS_OPEN: case UV_FS_READ: case UV_FS_WRITE: { JObject arg1(static_cast<int>(req->result)); jarg.Add(arg1); break; } case UV_FS_STAT: { uv_stat_t s = (req->statbuf); JObject ret(MakeStatObject(&s)); jarg.Add(ret); break; } default: jarg.Add(JObject::Null()); } } JObject res = MakeCallback(cb, JObject::Null(), jarg); uv_fs_req_cleanup(req); delete req_wrap; }
int JFont::compareTo(const JObject& s) const { if (className() != s.className()) return JObject::compareTo(s); JFont &obj = *(JFont*)&s; int result = name.compareTo(obj.name); if (!result) { result = style-obj.style; if (!result) result = size-obj.size; } return result; }
// Connection request result handler. static void AfterConnect(uv_connect_t* req, int status) { ConnectReqWrap* req_wrap = reinterpret_cast<ConnectReqWrap*>(req->data); TcpWrap* tcp_wrap = reinterpret_cast<TcpWrap*>(req->handle->data); IOTJS_ASSERT(req_wrap != NULL); IOTJS_ASSERT(tcp_wrap != NULL); // Take callback function object. // function afterConnect(status) JObject jcallback = req_wrap->jcallback(); IOTJS_ASSERT(jcallback.IsFunction()); // Only parameter is status code. JArgList args(1); args.Add(JVal::Number(status)); // Make callback. MakeCallback(jcallback, JObject::Null(), args); // Release request wrapper. delete req_wrap; }
static bool StartIoTjs(Environment* env) { // Get jerry global object. JObject global = JObject::Global(); // Bind environment to global object. global.SetNative((uintptr_t)(env), NULL); // Initialize builtin modules. JObject* process = InitModules(); // Call the entry. // load and call iotjs.js env->GoStateRunningMain(); RunIoTjs(process); // Run event loop. env->GoStateRunningLoop(); bool more; do { more = uv_run(env->loop(), UV_RUN_ONCE); more |= ProcessNextTick(); if (more == false) { more = uv_loop_alive(env->loop()); } } while (more); env->GoStateExiting(); // Emit 'exit' event. ProcessEmitExit(0); // Release bulitin modules. CleanupModules(); return true; }
int JInsets::compareTo(const JObject& s) const { if (className() != s.className()) return JObject::compareTo(s); JInsets &obj = *(JInsets*)&s; int result = top-obj.top; if (!result) { result = bottom-obj.bottom; if (!result) { result = left-obj.left; if (!result) result = right-obj.right; } } return result; }
void JObjectTree::Traverse( TraverseCallback callback, void* pContext ) const { if (!m_pRoot) return; float nodeW = m_NodeWidth; float nodeVisH = m_NodeHeight; Frame ext( m_RootPos.x, m_RootPos.y, nodeW, nodeVisH ); // find node path for tree expansion static std::vector<int> nodePath; nodePath.clear(); JObject* pCurObj = m_pExpanded; if (pCurObj) nodePath.push_back( 0 ); while (pCurObj && pCurObj != m_pRoot) { JObject* pParent = pCurObj->GetParent(); if (!pParent) break; nodePath.push_back( pParent->GetChildIndex( pCurObj ) ); pCurObj = pParent; } // traverse root node (which is never collapsed) if ((this->*callback)( ext, m_pRoot, (m_pExpanded != NULL), pContext ) == false) return; // traverse along expansion path pCurObj = m_pRoot; for (int i = nodePath.size() - 1; i >= 0; i--) { int childIdx = nodePath[i]; if (!pCurObj) break; int nCh = pCurObj->GetNChildren(); if (childIdx >= nCh) break; float blockH = float( nCh )*nodeVisH; ext.y = ext.y - blockH*0.5f + nodeVisH*0.5f; ext.x += nodeW; float cY = ext.y; for (int j = 0; j < nCh; j++) { JObject* pChild = pCurObj->GetChild( j ); Frame chExt( ext ); chExt.y = cY; bool bExpanded = (j == childIdx)&&(pCurObj != m_pExpanded); if ((this->*callback)( chExt, pChild, bExpanded, pContext ) == false) { return; } cY += nodeVisH; } pCurObj = pCurObj->GetChild( childIdx ); ext.y += float( childIdx )*nodeVisH; } } // JObjectTree::Traverse
int JLuaServer::FnDelete( lua_State* pLua ) { if (lua_gettop( pLua ) != 1) { rlog.err( "LUA: Incorrect command usage: <delete>. " "Function takes 1 object ref argument, but %d is provided", lua_gettop( pLua ) ); lua_settop( pLua, 0 ); lua_pushnil( pLua ); return 1; } // TODO: make this casting safe JObject* pObj = reinterpret_cast<JObject*>( lua_touserdata( pLua, -1 ) ); if (!pObj) { return 0; } // delete object pObj->Release(); lua_settop( pLua, 0 ); return 0; } // JLuaServer::FnDelete
int JDList::compareTo(const JObject& s) const { if (className() != s.className()) return JObject::compareTo(s); JDList &a = *(JDList*)&s; if (hnd == a.hnd) return 0; if (size() != a.size()) return size()-a.size(); JDCell* c = CELL(hnd); JDCell* d = CELL(a.hnd); do { if (*c->obj != *d->obj) return (c->obj)->compareTo(*d->obj); c = c->next; d = d->next; } while ((int)c != hnd); return 0; }
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); }
bool JObjectTree::DeleteNode() { if (!m_pSelected) return false; JObject* pParent = m_pSelected->GetParent(); if (!pParent) return false; int idx = pParent->GetChildIndex( m_pSelected ); pParent->RemoveChild( m_pSelected ); if (idx >= pParent->GetNChildren()) idx = pParent->GetNChildren() - 1; if (idx < 0) idx = 0; m_pSelected = pParent->GetChild( idx ); if (!m_pSelected) m_pSelected = pParent; m_pExpanded = m_pSelected; return true; } // JObjectTree::DeleteNode
std::string Proto::build_request( int clientno, int serverno, int messageid, long timestamp, size_t method_hash, OutSerializer& sout) { JObject* obj = new JObject(); obj->put(ClientNo, clientno); obj->put(ServerNo, serverno); obj->put(Version, 1); obj->put(Timestamp, timestamp); obj->put(MessageId, messageid); obj->put(Method, method_hash); obj->put(Param, sout.getContent()); std::string msg = dumps(obj); delete obj; return msg; }
JObject* InitBuffer() { Module* module = GetBuiltinModule(MODULE_BUFFER); JObject* buffer = module->module; if (buffer == NULL) { buffer = new JObject(Buffer); JObject prototype; buffer->SetProperty("prototype", prototype); prototype.SetMethod("compare", Compare); prototype.SetMethod("copy", Copy); prototype.SetMethod("write", Write); prototype.SetMethod("slice", Slice); prototype.SetMethod("toString", ToString); module->module = buffer; } return buffer; }
// 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); }
void JBoundsEditor::ClearEditors() { JObject* pEditors = GetEditorsGroup(); pEditors->RemoveChildren(); m_Edited.clear(); }
void JBoundsEditor::CreateEditor( JObject* pEdited ) { JObject* pTemplates = g_pObjectServer->FindObject( "templates", this ); JObject* pEditors = GetEditorsGroup(); if (pTemplates == NULL || pEditors == NULL) { return; } int nTemplates = pTemplates->GetNChildren(); JString editorName; for (int i = 0; i < nTemplates; i++) { JWidget* pTemplate = obj_cast<JWidget>( pTemplates->GetChild( i ) ); if (pTemplate && is_a( pEdited, pTemplate->GetName() )) { // this object can be edited with this editor template editorName = pEdited->GetName(); editorName += "_"; editorName += pTemplate->GetName(); editorName += "_editor"; JObject* pEditor = pTemplate->Clone( pEditors, editorName.c_str(), true ); pEditors->AddChild( pEditor ); JBoundsEditContext ctx; ctx.m_pEdited = pEdited; ctx.m_pEditor = pEditor; pEdited->GetPath( ctx.m_Path ); m_Edited.push_back( ctx ); // parse property connections from text field JString propcfg( pTemplate->GetText() ); char* propConfig = (char*)propcfg.c_str(); while (propConfig && *propConfig != 0) { char* editedVal = propConfig; char* editorVal = propConfig + strcspn( propConfig, "<|" ); char& ch = *editorVal; if (ch == '|') { ch = 0; editorVal = "value"; } else if (ch == '<') { *editorVal = 0; editorVal++; propConfig = editorVal + strcspn( editorVal, "|" ); if (*propConfig == '|') { *propConfig = 0; propConfig++; } } else if (ch == 0) { propConfig = 0; editorVal = "value"; } g_pSignalServer->Connect( pEditor, editorVal, pEdited, editedVal ); g_pSignalServer->Connect( pEdited, editedVal, pEditor, editorVal ); pEdited->SendSignal( editedVal ); g_pSignalServer->Connect( pEdited, "visible", pEditor, "visible" ); pEdited->SendSignal( "visible" ); } } } }
void JHandlerInfo::Return(JObject& ret) { ret.Ref(); *_ret_val_p = ret.raw_value(); }
void JavaUpload::globalInit() { if (!JVM::JVMAvailable()) return; // locate Java plugins try { JUploadPlugin::registerNatives(); JClass helper("info.dolezel.fatrat.plugins.helpers.NativeHelpers"); JClass annotation("info.dolezel.fatrat.plugins.annotations.UploadPluginInfo"); JClass annConfigDialog("info.dolezel.fatrat.plugins.annotations.ConfigDialog"); QList<QVariant> args; args << "info.dolezel.fatrat.plugins" << annotation.toVariant(); JArray arr = helper.callStatic("findAnnotatedClasses", JSignature().addString().add("java.lang.Class").retA("java.lang.Class"), args).value<JArray>(); qDebug() << "Found" << arr.size() << "annotated classes (UploadPluginInfo)"; int classes = arr.size(); for (int i = 0; i < classes; i++) { try { JClass obj = (jobject) arr.getObject(i); JObject ann = obj.getAnnotation(annotation); QString name = ann.call("name", JSignature().retString()).toString(); QString clsName = obj.getClassName(); JObject instance(obj, JSignature()); qDebug() << "Class name:" << clsName; qDebug() << "Name:" << name; JObject cfgDlg = obj.getAnnotation(annConfigDialog); JavaEngine e = { "EXT - " + name.toStdString(), clsName.toStdString() }; if (!cfgDlg.isNull()) e.configDialog = cfgDlg.call("value", JSignature().retString()).toString(); if (instance.instanceOf("info.dolezel.fatrat.plugins.extra.URLAcceptableFilter")) e.ownAcceptable = instance; if (instance.instanceOf("info.dolezel.fatrat.plugins.listeners.ConfigListener")) g_configListeners << instance; m_engines[clsName] = e; qDebug() << "createInstance of " << clsName; EngineEntry entry; entry.longName = m_engines[clsName].name.c_str(); entry.shortName = m_engines[clsName].shortName.c_str(); entry.lpfnAcceptable2 = JavaUpload::acceptable; entry.lpfnCreate2 = JavaUpload::createInstance; entry.lpfnInit = 0; entry.lpfnExit = 0; entry.lpfnMultiOptions = 0; addTransferClass(entry, Transfer::Upload); } catch (const RuntimeException& e) { qDebug() << e.what(); } } } catch (const RuntimeException& e) { qDebug() << e.what(); } }