示例#1
0
        private Engine CreateScriptEngine(ScriptContext context)
        {
            var engine = CreateScriptEngine();

            var contextInstance = new ObjectInstance(engine);

            if (context.Data != null)
            {
                contextInstance.FastAddProperty("data", new ContentDataObject(engine, context.Data), true, true, true);
            }

            if (context.OldData != null)
            {
                contextInstance.FastAddProperty("oldData", new ContentDataObject(engine, context.OldData), true, true, true);
            }

            if (context.User != null)
            {
                contextInstance.FastAddProperty("user", JintUser.Create(engine, context.User), false, true, false);
            }

            if (!string.IsNullOrWhiteSpace(context.Operation))
            {
                contextInstance.FastAddProperty("operation", context.Operation, false, true, false);
            }

            engine.SetValue("ctx", contextInstance);
            engine.SetValue("context", contextInstance);

            return(engine);
        }
        public bool TryConvert(Engine engine, object value, out JsValue result)
        {
            result = null;

            if (value is Enum)
            {
                result = value.ToString();
                return(true);
            }

            switch (value)
            {
            case IUser user:
                result = JintUser.Create(engine, user);
                return(true);

            case ClaimsPrincipal principal:
                result = JintUser.Create(engine, principal);
                return(true);

            case Instant instant:
                result = JsValue.FromObject(engine, instant.ToDateTimeUtc());
                return(true);

            case NamedContentData content:
                result = new ContentDataObject(engine, content);
                return(true);
            }

            return(false);
        }
示例#3
0
        public bool TryConvert(Engine engine, object value, [MaybeNullWhen(false)] out JsValue result)
        {
            result = null !;

            if (value is Enum)
            {
                result = value.ToString();
                return(true);
            }

            switch (value)
            {
            case IUser user:
                result = JintUser.Create(engine, user);
                return(true);

            case ClaimsPrincipal principal:
                result = JintUser.Create(engine, principal);
                return(true);

            case DomainId domainId:
                result = domainId.ToString();
                return(true);

            case Guid guid:
                result = guid.ToString();
                return(true);

            case Instant instant:
                result = JsValue.FromObject(engine, instant.ToDateTimeUtc());
                return(true);

            case Status status:
                result = status.ToString();
                return(true);

            case ContentData content:
                result = new ContentDataObject(engine, content);
                return(true);
            }

            return(false);
        }