// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using Microsoft.ClearScript.Util;
namespace Microsoft.ClearScript
{
///
/// Defines extension methods for use with all script engines.
///
public static class Extensions
{
///
/// Converts a type to a host type for use with script code currently running on the
/// calling thread.
///
/// The type to convert to a host type.
/// A host type for use with script code.
public static object ToHostType(this Type type)
{
return type.ToHostType(ScriptEngine.Current);
}
///
/// Converts a type to a host type for use with script code running in the specified
/// script engine.
///
/// The type to convert to a host type.
/// The script engine in which the host type will be used.
/// A host type for use with script code.
public static object ToHostType(this Type type, ScriptEngine engine)
{
MiscHelpers.VerifyNonNullArgument(type, nameof(type));
MiscHelpers.VerifyNonNullArgument(engine, nameof(engine));
return HostItem.Wrap(engine, HostType.Wrap(type));
}
///
/// Converts an object to a host object with the specified type restriction, for use with
/// script code currently running on the calling thread.
///
/// The type whose members are to be made accessible from script code.
/// The object to convert to a host object for use with script code.
/// A host object with the specified type restriction.
public static object ToRestrictedHostObject(this T target)
{
return target.ToRestrictedHostObject(ScriptEngine.Current);
}
///
/// Converts an object to a host object with the specified type restriction, for use with
/// script code running in the specified script engine.
///
/// The type whose members are to be made accessible from script code.
/// The object to convert to a host object for use with script code.
/// The script engine in which the host object will be used.
/// A host object with the specified type restriction.
public static object ToRestrictedHostObject(this T target, ScriptEngine engine)
{
MiscHelpers.VerifyNonNullArgument(target, nameof(target));
MiscHelpers.VerifyNonNullArgument(engine, nameof(engine));
return HostItem.Wrap(engine, target, typeof(T));
}
///
/// Converts an object to a host object with a type restriction specified as a
/// instance, for use with script code currently running on the
/// calling thread.
///
/// The object to convert to a host object for use with script code.
/// The type whose members are to be made accessible from script code.
/// A host object with the specified type restriction.
public static object ToRestrictedHostObject(this object target, Type type)
{
return target.ToRestrictedHostObject(type, ScriptEngine.Current);
}
///
/// Converts an object to a host object with a type restriction specified as a
/// instance, for use with script code running in the specified
/// script engine.
///
/// The object to convert to a host object for use with script code.
/// The type whose members are to be made accessible from script code.
/// The script engine in which the host object will be used.
/// A host object with the specified type restriction.
public static object ToRestrictedHostObject(this object target, Type type, ScriptEngine engine)
{
MiscHelpers.VerifyNonNullArgument(target, nameof(target));
MiscHelpers.VerifyNonNullArgument(type, nameof(type));
MiscHelpers.VerifyNonNullArgument(engine, nameof(engine));
if (!MiscHelpers.Try(out var holder, static type => Activator.CreateInstance(typeof(Holder<>).MakeGenericType(type)), type))
{
throw new ArgumentException("The specified type is invalid", nameof(type));
}
if (!MiscHelpers.Try(static ctx => ((IHolder)ctx.holder).Value = ctx.target, (holder, target)))
{
throw new ArgumentException("The target object is incompatible with the specified type", nameof(target));
}
return HostItem.Wrap(engine, target, type);
}
}
}