JObject* InitTcp() { Module* module = GetBuiltinModule(MODULE_TCP); JObject* tcp = module->module; if (tcp == NULL) { tcp = new JObject(TCP); JObject prototype; tcp->SetProperty("prototype", prototype); prototype.SetMethod("open", Open); prototype.SetMethod("close", Close); prototype.SetMethod("connect", Connect); prototype.SetMethod("bind", Bind); prototype.SetMethod("listen", Listen); prototype.SetMethod("write", Write); prototype.SetMethod("readStart", ReadStart); prototype.SetMethod("shutdown", Shutdown); prototype.SetMethod("setKeepAlive", SetKeepAlive); module->module = tcp; } return tcp; }
JObject* Gpio::GetJGpio() { Module* module = GetBuiltinModule(MODULE_GPIO); JObject* jgpio = module->module; IOTJS_ASSERT(jgpio != NULL); return jgpio; }
JObject* InitGpio() { Module* module = GetBuiltinModule(MODULE_GPIO); JObject* jgpio = module->module; if (jgpio == NULL) { jgpio = new JObject(); jgpio->SetMethod("initialize", Initialize); jgpio->SetMethod("release", Release); jgpio->SetMethod("setPin", SetPin); jgpio->SetMethod("writePin", WritePin); jgpio->SetMethod("readPin", ReadPin); jgpio->SetMethod("setPort", SetPort); jgpio->SetMethod("writePort", WritePort); jgpio->SetMethod("readPort", ReadPort); jgpio->SetMethod("query", Query); #define SET_CONSTANT(object, name, constant) \ do { \ JObject value(constant); \ object->SetProperty(name, value); \ } while (0) SET_CONSTANT(jgpio, "kGpioDirectionNone", kGpioDirectionNone); SET_CONSTANT(jgpio, "kGpioDirectionIn", kGpioDirectionIn); SET_CONSTANT(jgpio, "kGpioDirectionOut", kGpioDirectionOut); SET_CONSTANT(jgpio, "kGpioModeNone", kGpioModeNone); SET_CONSTANT(jgpio, "kGpioModePullup", kGpioModePullup); SET_CONSTANT(jgpio, "kGpioModePulldown", kGpioModePulldown); SET_CONSTANT(jgpio, "kGpioModeFloat", kGpioModeFloat); SET_CONSTANT(jgpio, "kGpioModePushpull", kGpioModePushpull); SET_CONSTANT(jgpio, "kGpioModeOpendrain", kGpioModeOpendrain); SET_CONSTANT(jgpio, "kGpioErrOk", kGpioErrOk); SET_CONSTANT(jgpio, "kGpioErrInitialize", kGpioErrInitialize); SET_CONSTANT(jgpio, "kGpioErrNotInitialized", kGpioErrNotInitialized); SET_CONSTANT(jgpio, "kGpioErrWrongUse", kGpioErrWrongUse); SET_CONSTANT(jgpio, "kGpioErrSysErr", kGpioErrSys); #undef SET_CONSTANT Gpio* gpio = Gpio::Create(*jgpio); IOTJS_ASSERT(gpio == reinterpret_cast<Gpio*>(jgpio->GetNative())); module->module = jgpio; } return jgpio; }
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; }
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(); }
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; }
JObject* InitConstants() { Module* module = GetBuiltinModule(MODULE_CONSTANTS); JObject* constants = module->module; if (constants == NULL) { constants = new JObject(); SET_CONSTANT(constants, O_APPEND); SET_CONSTANT(constants, O_CREAT); SET_CONSTANT(constants, O_EXCL); SET_CONSTANT(constants, O_RDONLY); SET_CONSTANT(constants, O_RDWR); SET_CONSTANT(constants, O_SYNC); SET_CONSTANT(constants, O_TRUNC); SET_CONSTANT(constants, O_WRONLY); SET_CONSTANT(constants, S_IFMT); SET_CONSTANT(constants, S_IFDIR); module->module = constants; } return constants; }