Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android V8 Support #290

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ClearScript/HostSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,14 @@ public static class HostSettings
/// resources such as native assemblies and related data files.
/// </remarks>
public static string AuxiliarySearchPath { get; set; }

/// <summary>
/// Sets the runtime platform to android and loads android native binaries.
/// </summary>
/// <remarks>
/// This property allows ClearScript to load android native assemblies instead of
/// linux assemblies on Android Mono
/// </remarks>
public static bool IsAndroid { get; set; }
}
}
7 changes: 6 additions & 1 deletion ClearScript/Util/MiscHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,12 @@ public static bool PlatformIsWindows()

public static bool PlatformIsLinux()
{
return RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
return !HostSettings.IsAndroid && RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
}

public static bool PlatformIsAndroid()
{
return HostSettings.IsAndroid;
}

public static bool PlatformIsOSX()
Expand Down
64 changes: 59 additions & 5 deletions ClearScript/Util/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public static IntPtr LoadLibrary(string path)
return NativeWindowsMethods.LoadLibraryW(path);
}

if (MiscHelpers.PlatformIsAndroid())
{
return NativeAndroidMethods.LoadLibrary(path);
}

if (MiscHelpers.PlatformIsLinux())
{
return NativeLinuxMethods.LoadLibrary(path);
Expand All @@ -34,6 +39,11 @@ public static bool FreeLibrary(IntPtr hLibrary)
if (MiscHelpers.PlatformIsWindows())
{
return NativeWindowsMethods.FreeLibrary(hLibrary);
}

if (MiscHelpers.PlatformIsAndroid())
{
return NativeAndroidMethods.FreeLibrary(hLibrary) == 0;
}

if (MiscHelpers.PlatformIsLinux())
Expand All @@ -54,6 +64,11 @@ public static string GetLoadLibraryErrorMessage()
if (MiscHelpers.PlatformIsWindows())
{
return new Win32Exception().Message;
}

if (MiscHelpers.PlatformIsAndroid())
{
return Marshal.PtrToStringAnsi(NativeAndroidMethods.GetLoadLibraryErrorMessage());
}

if (MiscHelpers.PlatformIsLinux())
Expand Down Expand Up @@ -254,12 +269,12 @@ [Out] out uint oldProtect
);

// ReSharper restore MemberHidesStaticFromOuterClass
}

}
#endregion

#region Nested type: NativeLinuxMethods

private static class NativeLinuxMethods
{
// ReSharper disable MemberHidesStaticFromOuterClass
Expand All @@ -284,7 +299,46 @@ public enum LoadLibraryFlags

[DllImport("libdl.so", EntryPoint = "dlopen")]
public static extern IntPtr LoadLibrary(
[In] [MarshalAs(UnmanagedType.LPStr)] string path,
[In][MarshalAs(UnmanagedType.LPStr)] string path,
[In] LoadLibraryFlags flags = LoadLibraryFlags.Now | LoadLibraryFlags.Global
);

[DllImport("libdl.so", EntryPoint = "dlclose")]
public static extern int FreeLibrary(
[In] IntPtr hLibrary
);

[DllImport("libdl.so", EntryPoint = "dlerror")]
public static extern IntPtr GetLoadLibraryErrorMessage();

// ReSharper restore MemberHidesStaticFromOuterClass
}

#endregion

#region Nested type: NativeLinuxMethods

private static class NativeAndroidMethods
{
// ReSharper disable MemberHidesStaticFromOuterClass

[Flags]
public enum LoadLibraryFlags
{
// ReSharper disable UnusedMember.Local

None = 0,
Now = 0,
Lazy = 1,
Local = 0,
Global = 2,

// ReSharper restore UnusedMember.Local
}

[DllImport("libdl.so", EntryPoint = "dlopen")]
public static extern IntPtr LoadLibrary(
[In][MarshalAs(UnmanagedType.LPStr)] string path,
[In] LoadLibraryFlags flags = LoadLibraryFlags.Now | LoadLibraryFlags.Global
);

Expand Down
6 changes: 2 additions & 4 deletions ClearScript/V8/SplitProxy/V8SplitProxyNative.Common.tt
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,19 @@ namespace Microsoft.ClearScript.V8.SplitProxy
{
private static IV8SplitProxyNative CreateInstance()
{
var architecture = RuntimeInformation.ProcessArchitecture;

<#
foreach (var osPlatform in platforms.Select(testPlatform => testPlatform.Item1).Distinct())
{
#>

if (RuntimeInformation.IsOSPlatform(OSPlatform.<#= osPlatform #>))
if (IsOSPlatform("<#= osPlatform #>"))
{
<#
foreach (var platform in platforms.Where(testPlatform => testPlatform.Item1 == osPlatform))
{
#>

if (architecture == Architecture.<#= platform.Item2 #>)
if (IsArchitecture("<#= osPlatform #>", "<#= platform.Item2 #>"))
{
return new <#= $"Impl_{osPlatform}_{platform.Item2}" #>();
}
Expand Down
Loading