void CJSKadID::FxToString(const v8::FunctionCallbackInfo<v8::Value> &args) { v8::HandleScope HandleScope(v8::Isolate::GetCurrent()); CJSKadID* jKadID = GetJSObject<CJSKadID>(args.Holder()); args.GetReturnValue().Set(v8::String::NewFromTwoByte(v8::Isolate::GetCurrent(), V8STR(jKadID->m_pKadID->m_Value.ToHex().c_str()))); }
/** toString: * convert the buffer into a hex encoded string * * @return string * Hex Encoded buffer content */ void CJSBuffer::FxToString(const v8::FunctionCallbackInfo<v8::Value> &args) { v8::HandleScope HandleScope(v8::Isolate::GetCurrent()); CBufferObj* pBuffer = GetCObject<CBufferObj>(args.Holder()); args.GetReturnValue().Set(v8::String::NewFromTwoByte(v8::Isolate::GetCurrent(), V8STR(ToHex(pBuffer->GetBuffer(), pBuffer->GetSize()).c_str()))); }
PJsonVal TNodeJsStreamAggr::SaveJson(const int& Limit) const { if (!SaveJsonFun.IsEmpty()) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); v8::Local<v8::Function> Callback = v8::Local<v8::Function>::New(Isolate, SaveJsonFun); v8::Local<v8::Object> GlobalContext = Isolate->GetCurrentContext()->Global(); const unsigned Argc = 1; v8::Local<v8::Value> ArgV[Argc] = { v8::Number::New(Isolate, Limit) }; v8::TryCatch TryCatch; v8::Local<v8::Value> ReturnVal = Callback->Call(GlobalContext, Argc, ArgV); if (TryCatch.HasCaught()) { TryCatch.ReThrow(); return TJsonVal::NewObj(); } QmAssertR(ReturnVal->IsObject(), "Stream aggr JS callback: saveJson didn't return an object."); PJsonVal Res = TNodeJsUtil::GetObjJson(ReturnVal->ToObject()); QmAssertR(Res->IsDef(), "Stream aggr JS callback: saveJson didn't return a valid JSON."); return Res; } else { return TJsonVal::NewObj(); } }
v8::Local<v8::ObjectTemplate> CJSBuffer::Prepare() { v8::EscapableHandleScope HandleScope(v8::Isolate::GetCurrent()); v8::Local<v8::ObjectTemplate> InstanceTemplate = v8::ObjectTemplate::New(); InstanceTemplate->SetInternalFieldCount(2); InstanceTemplate->Set(v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), (uint8_t*)"read"),v8::FunctionTemplate::New(v8::Isolate::GetCurrent(), FxRead),v8::ReadOnly); InstanceTemplate->Set(v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), (uint8_t*)"write"),v8::FunctionTemplate::New(v8::Isolate::GetCurrent(), FxWrite),v8::ReadOnly); InstanceTemplate->Set(v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), (uint8_t*)"readValue"),v8::FunctionTemplate::New(v8::Isolate::GetCurrent(), FxReadValue),v8::ReadOnly); InstanceTemplate->Set(v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), (uint8_t*)"writeValue"),v8::FunctionTemplate::New(v8::Isolate::GetCurrent(), FxWriteValue),v8::ReadOnly); InstanceTemplate->Set(v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), (uint8_t*)"readString"),v8::FunctionTemplate::New(v8::Isolate::GetCurrent(), FxReadString),v8::ReadOnly); InstanceTemplate->Set(v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), (uint8_t*)"writeString"),v8::FunctionTemplate::New(v8::Isolate::GetCurrent(), FxWriteString),v8::ReadOnly); InstanceTemplate->Set(v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), (uint8_t*)"seek"),v8::FunctionTemplate::New(v8::Isolate::GetCurrent(), FxSeek),v8::ReadOnly); InstanceTemplate->Set(v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), (uint8_t*)"resize"),v8::FunctionTemplate::New(v8::Isolate::GetCurrent(), FxResize),v8::ReadOnly); InstanceTemplate->SetAccessor(v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), (uint8_t*)"length"), GetLength, 0, v8::Local<v8::Value>(), v8::DEFAULT, v8::ReadOnly); InstanceTemplate->SetAccessor(v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), (uint8_t*)"position"), GetPosition, 0, v8::Local<v8::Value>(), v8::DEFAULT, v8::ReadOnly); InstanceTemplate->Set(v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), (uint8_t*)"toString"), v8::FunctionTemplate::New(v8::Isolate::GetCurrent(), FxToString),v8::ReadOnly); v8::Local<v8::ObjectTemplate> Template = v8::Local<v8::ObjectTemplate>::New(v8::Isolate::GetCurrent(), InstanceTemplate); m_Template.Reset(v8::Isolate::GetCurrent(), Template); v8::Local<v8::ObjectTemplate> ConstructorTemplate = v8::ObjectTemplate::New(); ConstructorTemplate->SetInternalFieldCount(2); ConstructorTemplate->SetCallAsFunctionHandler(CJSBuffer::JSBuffer); return HandleScope.Escape(ConstructorTemplate); }
/** writeValue: * write a value of a given type to the buffer * * @param string type * value type to be read: uint64, int64/sint64, uint32, int32/sint32, uint16, int16/sint16, uint8/byte, int8/sint8/char * * @param value to be writen */ void CJSBuffer::FxWriteValue(const v8::FunctionCallbackInfo<v8::Value> &args) { v8::HandleScope HandleScope(v8::Isolate::GetCurrent()); CJSBuffer* jBuffer = GetJSObject<CJSBuffer>(args.Holder()); CBufferObj* pBuffer = jBuffer->m_pBuffer; if (args.Length() < 2) { args.GetIsolate()->ThrowException(v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), (uint8_t*)"Invalid Argument")); return; } BUFFER_TRY wstring Type = CJSEngine::GetWStr(args[0]); if(CompareStr(Type, L"uint64")) pBuffer->WriteValue<uint64>(args[1]->NumberValue()); else if(CompareStr(Type, L"int64") || CompareStr(Type, L"sint64")) pBuffer->WriteValue<sint64>(args[1]->NumberValue()); else if(CompareStr(Type, L"uint32")) pBuffer->WriteValue<uint32>(args[1]->Uint32Value()); else if(CompareStr(Type, L"int32") || CompareStr(Type, L"sint32")) pBuffer->WriteValue<sint32>(args[1]->Int32Value()); else if(CompareStr(Type, L"uint16")) pBuffer->WriteValue<uint16>(args[1]->Uint32Value()); else if(CompareStr(Type, L"int16") || CompareStr(Type, L"sint16")) pBuffer->WriteValue<sint16>(args[1]->Int32Value()); else if(CompareStr(Type, L"uint8") || CompareStr(Type, L"byte")) pBuffer->WriteValue<uint8>(args[1]->Uint32Value()); else if(CompareStr(Type, L"int8") || CompareStr(Type, L"sint8") || CompareStr(Type, L"char")) pBuffer->WriteValue<sint8>(args[1]->Int32Value()); else if(CompareStr(Type, L"str")) pBuffer->WriteString(CJSEngine::GetWStr(args[1])); else args.GetIsolate()->ThrowException(v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), (uint8_t*)"Invalid Argument")); BUFFER_CATCH }
void TNodeJsFIn::Init(v8::Handle<v8::Object> exports) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); // template for creating function from javascript using "new", uses _NewJs callback v8::Local<v8::FunctionTemplate> tpl = v8::FunctionTemplate::New(Isolate, TNodeJsUtil::_NewJs<TNodeJsFIn>); // child will have the same properties and methods, but a different callback: _NewCpp v8::Local<v8::FunctionTemplate> child = v8::FunctionTemplate::New(Isolate, TNodeJsUtil::_NewCpp<TNodeJsFIn>); child->Inherit(tpl); child->SetClassName(v8::String::NewFromUtf8(Isolate, GetClassId().CStr())); // ObjectWrap uses the first internal field to store the wrapped pointer child->InstanceTemplate()->SetInternalFieldCount(1); tpl->SetClassName(v8::String::NewFromUtf8(Isolate, GetClassId().CStr())); // ObjectWrap uses the first internal field to store the wrapped pointer tpl->InstanceTemplate()->SetInternalFieldCount(1); // Add all prototype methods, getters and setters here NODE_SET_PROTOTYPE_METHOD(tpl, "peekCh", _peekCh); NODE_SET_PROTOTYPE_METHOD(tpl, "getCh", _getCh); NODE_SET_PROTOTYPE_METHOD(tpl, "readLine", _readLine); NODE_SET_PROTOTYPE_METHOD(tpl, "readJson", _readJson); NODE_SET_PROTOTYPE_METHOD(tpl, "readAll", _readAll); // Add properties tpl->InstanceTemplate()->SetAccessor(v8::String::NewFromUtf8(Isolate, "eof"), _eof); tpl->InstanceTemplate()->SetAccessor(v8::String::NewFromUtf8(Isolate, "length"), _length); // This has to be last, otherwise the properties won't show up on the object in JavaScript // Constructor is used when creating the object from C++ Constructor.Reset(Isolate, child->GetFunction()); // we need to export the class for calling using "new FIn(...)" exports->Set(v8::String::NewFromUtf8(Isolate, GetClassId().CStr()), tpl->GetFunction()); }
void TNodeJsFIn::New(const v8::FunctionCallbackInfo<v8::Value>& Args) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); if (Args.IsConstructCall()) { EAssertR(Args.Length() == 1 && Args[0]->IsString(), "Expected a file path."); TStr FNm(*v8::String::Utf8Value(Args[0]->ToString())); EAssertR(TFile::Exists(FNm), "File does not exist."); TNodeJsFIn* JsFIn = new TNodeJsFIn(FNm); v8::Local<v8::Object> Instance = Args.This(); v8::Handle<v8::String> key = v8::String::NewFromUtf8(Isolate, "class"); v8::Handle<v8::String> value = v8::String::NewFromUtf8(Isolate, ClassId.CStr()); Instance->SetHiddenValue(key, value); JsFIn->Wrap(Instance); Args.GetReturnValue().Set(Instance); } else { const int Argc = 1; v8::Local<v8::Value> Argv[Argc] = { Args[0] }; v8::Local<v8::Function> cons = v8::Local<v8::Function>::New(Isolate, constructor); v8::Local<v8::Object> Instance = cons->NewInstance(Argc, Argv); Args.GetReturnValue().Set(Instance); } }
TNodeJsRf24Radio* TNodeJsRf24Radio::NewFromArgs(const v8::FunctionCallbackInfo<v8::Value>& Args) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); PJsonVal ParamJson = TNodeJsUtil::GetArgJson(Args, 0); const int PinCE = ParamJson->GetObjInt("pinCE"); const int PinCSN = ParamJson->GetObjInt("pinCSN"); const uint16 MyId = (uint16) ParamJson->GetObjInt("id"); const PJsonVal SensorJsonV = ParamJson->GetObjKey("sensors"); const bool Verbose = ParamJson->GetObjBool("verbose", false); const PNotify Notify = Verbose ? TNotify::StdNotify : TNotify::NullNotify; Notify->OnNotify(TNotifyType::ntInfo, "Parsing configuration ..."); TStrIntH SensorNmIdH; TStrIntH SensorIdNodeIdH; for (int SensorN = 0; SensorN < SensorJsonV->GetArrVals(); SensorN++) { const PJsonVal SensorJson = SensorJsonV->GetArrVal(SensorN); const TStr& SensorId = SensorJson->GetObjStr("id"); SensorNmIdH.AddDat(SensorId, SensorJson->GetObjInt("internalId")); SensorIdNodeIdH.AddDat(SensorId, SensorJson->GetObjInt("nodeId")); } Notify->OnNotify(TNotifyType::ntInfo, "Calling cpp constructor ..."); return new TNodeJsRf24Radio(MyId, PinCE, PinCSN, SensorNmIdH, SensorIdNodeIdH, Notify); }
void TNodeJsFOut::New(const v8::FunctionCallbackInfo<v8::Value>& Args) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::EscapableHandleScope HandleScope(Isolate); if (Args.IsConstructCall()) { EAssertR(Args.Length() >= 1 && Args[0]->IsString(), "Expected file path."); TStr FNm(*v8::String::Utf8Value(Args[0]->ToString())); bool AppendP = Args.Length() >= 2 && Args[1]->IsBoolean() && Args[1]->BooleanValue(); TNodeJsFOut* JsFOut = new TNodeJsFOut(FNm, AppendP); v8::Local<v8::Object> Instance = Args.This(); JsFOut->Wrap(Instance); Args.GetReturnValue().Set(Instance); } else { const int Argc = 1; v8::Local<v8::Value> Argv[Argc] = { Args[0] }; v8::Local<v8::Function> cons = v8::Local<v8::Function>::New(Isolate, constructor); cons->NewInstance(Argc, Argv); v8::Local<v8::Object> Instance = cons->NewInstance(Argc, Argv); Args.GetReturnValue().Set(Instance); } }
void TNodeJsFIn::getCh(const v8::FunctionCallbackInfo<v8::Value>& Args) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); TNodeJsFIn* JsFIn = ObjectWrap::Unwrap<TNodeJsFIn>(Args.This()); Args.GetReturnValue().Set(v8::String::NewFromUtf8(Isolate, TStr(JsFIn->SIn->GetCh()).CStr())); }
void TNodeJsFs::exists(const v8::FunctionCallbackInfo<v8::Value>& Args) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); EAssertR(Args.Length() == 1 && Args[0]->IsString(), "Expected file path."); TStr FNm(*v8::String::Utf8Value(Args[0]->ToString())); Args.GetReturnValue().Set(v8::Boolean::New(Isolate, TFile::Exists(FNm))); }
TNodejsDHT11Sensor::TReadTask::TReadTask(const v8::FunctionCallbackInfo<v8::Value>& Args): TNodeTask(Args), JsSensor(nullptr) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); JsSensor = ObjectWrap::Unwrap<TNodejsDHT11Sensor>(Args.Holder()); }
void TNodeJsFIn::length(v8::Local<v8::String> Name, const v8::PropertyCallbackInfo<v8::Value>& Info) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); TNodeJsFIn* JsFIn = ObjectWrap::Unwrap<TNodeJsFIn>(Info.Holder()); Info.GetReturnValue().Set(v8::Integer::New(Isolate, JsFIn->SIn->Len())); }
void TNodeJsFs::openAppend(const v8::FunctionCallbackInfo<v8::Value>& Args) { // Call with AppendP = true v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); EAssertR(Args.Length() == 1 && Args[0]->IsString(), "Expected file path."); TStr FNm(*v8::String::Utf8Value(Args[0]->ToString())); Args.GetReturnValue().Set(TNodeJsFOut::New(FNm, true)); }
void TNodeJsSA::name(v8::Local<v8::String> Name, const v8::PropertyCallbackInfo<v8::Value>& Info) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); v8::Local<v8::Object> Self = Info.Holder(); TNodeJsSA* JsSA = ObjectWrap::Unwrap<TNodeJsSA>(Self); Info.GetReturnValue().Set(v8::String::NewFromUtf8(Isolate, JsSA->SA->GetAggrNm().CStr())); }
void TNodeTask::AfterRunSync(const v8::FunctionCallbackInfo<v8::Value>& Args) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); if (!Except.Empty()) { throw Except; } Args.GetReturnValue().Set(WrapResult()); }
void CJSKadID::FxToVariant(const v8::FunctionCallbackInfo<v8::Value> &args) { v8::HandleScope HandleScope(v8::Isolate::GetCurrent()); CJSKadID* jKadID = GetJSObject<CJSKadID>(args.Holder()); CJSObject* jObject = CJSVariant::New(new CVariantPrx(jKadID->m_pKadID->m_Value), jKadID->m_pScript); args.GetReturnValue().Set(jObject->GetInstance()); }
void TNodeJsSA::val(v8::Local<v8::String> Name, const v8::PropertyCallbackInfo<v8::Value>& Info) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); v8::Local<v8::Object> Self = Info.Holder(); TNodeJsSA* JsSA = ObjectWrap::Unwrap<TNodeJsSA>(Self); Info.GetReturnValue().Set(TNodeJsUtil::ParseJson(Isolate, JsSA->SA->SaveJson(-1))); }
v8::Handle<v8::Function> TNodeJsUtil::GetArgFun(const v8::FunctionCallbackInfo<v8::Value>& Args, const int& ArgN) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); EAssertR(IsArgFun(Args, ArgN), "The specified argument is not a function!"); return v8::Handle<v8::Function>::Cast(Args[ArgN]); }
uint64 TNodeJsUtil::GetArgTmMSecs(const v8::FunctionCallbackInfo<v8::Value>& Args, const int& ArgN) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); EAssert(!IsArgUndef(Args, ArgN)); v8::Handle<v8::Value> Val = Args[ArgN]; return GetTmMSecs(Val); }
double TNodeJsUtil::GetFldFlt(v8::Local<v8::Object> Obj, const TStr& FldNm) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); EAssertR(IsFldFlt(Obj, FldNm), "The field is not an integer!"); v8::Local<v8::Value> FldVal = Obj->Get(v8::String::NewFromUtf8(Isolate, FldNm.CStr())); return FldVal->NumberValue(); }
double TNodeJsUtil::GetArgFlt(const v8::FunctionCallbackInfo<v8::Value>& Args, const int& ArgN, const double& DefVal) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); if (ArgN >= Args.Length()) { return DefVal; } v8::Handle<v8::Value> Val = Args[ArgN]; EAssertR(Val->IsNumber(), TStr::Fmt("Argument %d expected to be number", ArgN)); return Val->NumberValue(); }
int TNodeJsUtil::GetArgInt32(const v8::FunctionCallbackInfo<v8::Value>& Args, const int& ArgN) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); EAssertR(Args.Length() > ArgN, TStr::Fmt("TNodeJsUtil::GetArgInt32: Missing argument %d", ArgN)); v8::Handle<v8::Value> Val = Args[ArgN]; EAssertR(Val->IsInt32(), TStr::Fmt("Argument %d expected to be int", ArgN)); return Val->Int32Value(); }
bool TNodeJsUtil::IsBuffer(const v8::Local<v8::Object>& Object) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); #if NODE_MODULE_VERSION >= 46 /* Node.js >= v4.x.x */ return Object->IsUint8Array(); #else return Object->HasIndexedPropertiesInExternalArrayData(); #endif }
bool TNodeJsUtil::IsArgJson(const v8::FunctionCallbackInfo<v8::Value>& Args, const int& ArgN) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); EAssertR(Args.Length() > ArgN, TStr::Fmt("Missing argument %d", ArgN).CStr()); v8::Handle<v8::Value> Val = Args[ArgN]; return Val->IsObject(); }
bool TNodeJsUtil::IsArgUndef(const v8::FunctionCallbackInfo<v8::Value>& Args, const int& ArgN) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); if (Args.Length() <= ArgN) { return true; } v8::Handle<v8::Value> Val = Args[ArgN]; return Val->IsUndefined(); }
void TNodejsDHT11Sensor::init(const v8::FunctionCallbackInfo<v8::Value>& Args) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); TNodejsDHT11Sensor* JsSensor = ObjectWrap::Unwrap<TNodejsDHT11Sensor>(Args.Holder()); JsSensor->Sensor->Init(); Args.GetReturnValue().Set(v8::Undefined(Isolate)); }
void TNodeJsFIn::readLine(const v8::FunctionCallbackInfo<v8::Value>& Args) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); TNodeJsFIn* JsFIn = ObjectWrap::Unwrap<TNodeJsFIn>(Args.This()); TChA LnChA; JsFIn->SIn->GetNextLnBf(LnChA); Args.GetReturnValue().Set(v8::String::NewFromUtf8(Isolate, LnChA.CStr())); }
void TNodeJsFOut::close(const v8::FunctionCallbackInfo<v8::Value>& Args) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); TNodeJsFOut* JsFOut = ObjectWrap::Unwrap<TNodeJsFOut>(Args.This()); JsFOut->SOut.Clr(); Args.GetReturnValue().Set(Args.Holder()); }
TStr TNodeJsUtil::GetClass(const v8::Handle<v8::Object> Obj) { v8::Isolate* Isolate = v8::Isolate::GetCurrent(); v8::HandleScope HandleScope(Isolate); v8::Local<v8::Value> ClassNm = Obj->GetHiddenValue(v8::String::NewFromUtf8(Isolate, "class")); const bool EmptyP = ClassNm.IsEmpty(); if (EmptyP) { return ""; } v8::String::Utf8Value Utf8(ClassNm); return TStr(*Utf8); }