public TwiDevice(Topic owner, TWI twi) { this.owner = owner; this._twi = twi; JSC.JSValue jSrc; var jType = owner.GetField("type"); Topic tt; if (jType.ValueType == JSC.JSValueType.String && jType.Value != null && Topic.root.Get("$YS/TYPES", false).Exist(jType.Value as string, out tt) && (jSrc = JsLib.GetField(tt.GetState(), "src")).ValueType == JSC.JSValueType.String) { } else { jSrc = null; } if (jSrc != null) { try { _ctx = new JSC.Context(JsExtLib.Context); _ctx.DefineVariable("setTimeout").Assign(JSC.JSValue.Marshal(new Func <JSC.JSValue, int, JSC.JSValue>(SetTimeout))); _ctx.DefineVariable("setInterval").Assign(JSC.JSValue.Marshal(new Func <JSC.JSValue, int, JSC.JSValue>(SetInterval))); _ctx.DefineVariable("setAlarm").Assign(JSC.JSValue.Marshal(new Func <JSC.JSValue, JSC.JSValue, JSC.JSValue>(SetAlarm))); var f = _ctx.Eval(jSrc.Value as string) as JSL.Function; if (f != null) { if (f.RequireNewKeywordLevel == JSL.RequireNewKeywordLevel.WithNewOnly) { this._self = JSC.JSObject.create(new JSC.Arguments { f.prototype }); } else { this._self = JSC.JSObject.CreateObject(); } _self["GetState"] = JSC.JSValue.Marshal(new Func <string, JSC.JSValue>(GetState)); _self["SetState"] = JSC.JSValue.Marshal(new Action <string, JSC.JSValue>(SetState)); _self["GetField"] = JSC.JSValue.Marshal(new Func <string, string, JSC.JSValue>(GetField)); _self["TwiReq"] = JSC.JSValue.Marshal(new Func <int[], Task <JSC.JSValue> >(_twi.TwiReq)); if (f.RequireNewKeywordLevel == JSL.RequireNewKeywordLevel.WithNewOnly) { _self = f.Construct(_self, new JSC.Arguments()); } else { f.Call(_self, new JSC.Arguments()); // Call constructor } } } catch (Exception ex) { Log.Warning("{0}.ctor() - {1}", owner.path, ex.Message); } } else { Log.Warning("{0} constructor is not defined", owner.path); } }
public override JSValue DefineVariable(string name, bool deletable) { return(prototype.DefineVariable(name)); }
public void ManifestChanged() { JSC.JSValue jSrc; var jType = _owner.GetField("type"); Topic tt; if (jType.ValueType == JSC.JSValueType.String && jType.Value != null && Topic.root.Get("$YS/TYPES", false).Exist(jType.Value as string, out tt) && _typeT != tt && (jSrc = JsLib.GetField(tt.GetState(), "src")).ValueType == JSC.JSValueType.String) { _typeT = tt; } else { jSrc = null; } if (jSrc != null) { try { _ctx = new JSC.Context(JsExtLib.Context); _ctx.DefineVariable("setTimeout").Assign(JSC.JSValue.Marshal(new Func <JSC.JSValue, int, JSC.JSValue>(SetTimeout))); _ctx.DefineVariable("setInterval").Assign(JSC.JSValue.Marshal(new Func <JSC.JSValue, int, JSC.JSValue>(SetInterval))); _ctx.DefineVariable("setAlarm").Assign(JSC.JSValue.Marshal(new Func <JSC.JSValue, JSC.JSValue, JSC.JSValue>(SetAlarm))); var f = _ctx.Eval(jSrc.Value as string) as JSL.Function; if (f != null) { if (f.RequireNewKeywordLevel == JSL.RequireNewKeywordLevel.WithNewOnly) { this._self = JSC.JSObject.create(new JSC.Arguments { f.prototype }); } else { this._self = JSC.JSObject.CreateObject(); } var cf = _self.GetProperty("Calculate"); _calcFunc = (cf as JSL.Function) ?? (cf.Value as JSL.Function); _self["GetState"] = JSC.JSValue.Marshal(new Func <string, JSC.JSValue>(GetState)); _self["SetState"] = JSC.JSValue.Marshal(new Action <string, JSC.JSValue>(SetState)); _self["GetField"] = JSC.JSValue.Marshal(new Func <string, string, JSC.JSValue>(GetField)); if (f.RequireNewKeywordLevel == JSL.RequireNewKeywordLevel.WithNewOnly) { _self = f.Construct(_self, new JSC.Arguments()); } else { f.Call(_self, new JSC.Arguments()); // Call constructor } } } catch (Exception ex) { Log.Warning("{0}.ctor() - {1}", _owner.path, ex.Message); } } else { if (!_owner.disposed) { Log.Warning("{0} constructor is not defined", _owner.path); } } }